【问题标题】:kivy: How to locate a scroll label in FloatLayout?kivy:如何在 FloatLayout 中定位滚动标签?
【发布时间】:2016-08-19 22:52:36
【问题描述】:

我无法在 FloatLayout 中找到 scroll label

原始代码来自 Alexander Taylor。链接:https://github.com/kivy/kivy/wiki/Scrollable-Label

我想将scroll label 定位为如图所示。

位置值是:

vtop = 屏幕高度 / 10

vbottom = 屏幕高度 / 20

hleft = 屏幕宽度 / 20

hright = 屏幕宽度 / 40

我在原始代码中添加了一个 FloatLayout。这是我更改的代码。

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

long_text = 'yay moo cow foo bar moo baa ' * 200

Builder.load_string('''
<ScrollableLabel>:
    FloatLayout:
        Label:
            size: root.width - root.width/20 - root.width/40, root.height - root.height/10 - root.height/20
            pos: root.width /20, root.height / 20
            #size_hint: (None, None)
#            valign: 'top'
            size_hint_y: None
            height: self.texture_size[1]
            text_size: self.width - self.width/20 - self.width/40, self.height - self.height/10 - self.height/20
#            text_size: self.width , None
            font_size: 30
            text: root.text
''')

class ScrollableLabel(ScrollView):
    text = StringProperty('')

class ScrollApp(App):
    def build(self):
        return ScrollableLabel(text=long_text)

if __name__ == "__main__":
    ScrollApp().run()

我尝试了一些方法来定位滚动标签,正如您在上面的代码中看到的那样,但它没有给我所需的结果。

我会询问您的想法以使代码正确。谢谢!

【问题讨论】:

    标签: kivy


    【解决方案1】:

    我认为您的问题是您的应用程序的主要小部件是从 ScrollView 派生的,而您只希望内部区域可滚动。您在 ScrollView 中有您的 FloatLayout。我建议使用主布局小部件并将 ScrollableLabel 嵌入其中的方法。

    from kivy.app import App
    from kivy.uix.label import Label
    from kivy.uix.scrollview import ScrollView
    from kivy.uix.floatlayout import FloatLayout
    from kivy.properties import StringProperty
    from kivy.lang import Builder
    
    long_text = 'yay moo cow foo bar moo baa ' * 100
    
    Builder.load_string('''
    <MainApp>:
        ScrollableLabel:
            id: scrollable
            pos: self.parent.pos[0]+self.parent.size[0]/20, self.parent.pos[1]+self.parent.size[1]/20
            size_hint: 18.5 / 20.0, 0.85
    
    <ScrollableLabel>:
        Label:
            size_hint_y: None
            height: self.texture_size[1]
            text_size: self.width, None
            text: root.text
    ''')
    
    class MainApp(FloatLayout):
        pass
    
    class ScrollableLabel(ScrollView):
        text = StringProperty('')
    
    class ScrollApp(App):
        def build(self):
            app = MainApp()
            app.ids["scrollable"].text = long_text
            return app
    
    if __name__ == "__main__":
        ScrollApp().run()
    

    【讨论】:

    • 非常感谢!有用!但我可以问为什么'size_hint: 18.5 / 20.0, 0.85'?我不明白这里的价值观。
    • 啊,是的,即根据您指定的边距从 1 - (1/20) - (1/40)、1 - (1/10) - (1/20)。跨度>
    • 很抱歉再次打扰您。我想知道是否有任何方法可以将可滚动标签放置在 FloatLayout 中?因为我在应用程序中有多个屏幕。请参阅:link 我在第二个屏幕中有可滚动标签。如果我有一个额外的 MainApp,我不知道如何设置标签值。
    • 我处理多个页面的方法是拥有一个 MainLayout(或 MainApp 小部件),然后通过在 MainLayout 上调用 remove_widget() 删除一个小部件“页面”,然后通过 add_widget( )。通常,每个页面也派生自一个标准布局小部件类。 (Kivy 似乎没有像其他 GUI 一样隐藏小部件的概念。)如果您想了解更多细节,请在 stackoverflow 上提出一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2013-03-29
    相关资源
    最近更新 更多