【问题标题】:Why Kivy ScrollView children of child are not scrollable?为什么孩子的 Kivy ScrollView 孩子不可滚动?
【发布时间】:2021-07-24 21:19:47
【问题描述】:

这是运行 kivy 的 python 脚本的一部分

class someclass(Widget):
# code
# code
Clock.schedule_interval(self.timeandlog, 0.1)
self.x = 20
def timeandlog(self,dt):
    if condition == True: # 
       self.ids.pofchild.add_widget(Label(text=logmsg, pos = (10, self.x)))
       self.x = self.x + 10   ### just playing with position 
       condition = False  

kv 文件:

<someclass>

    #somelabels and buttons:

    ScrollView:
        do_scroll_x: False
        do_scroll_y: True
        pos: root.width*0.3, root.height*0.7
        size: root.width*0.8, root.height*0.7 
        Widget:
            cols: 1 
            spacing: 10
            id: pofchild

现在我知道ScrollView 接受一个Widget,所以我只添加了一个id: pofchild,然后我在其中添加了self.ids.pofchild.add_widget(Label() 的标签,并将每个新标签的pos 更改为pos=(20, self.x),但标签不可滚动,仅填充小部件高度然后停止出现。什么是正确的属性,以便它们可以滚动?

【问题讨论】:

    标签: python kivy scrollview


    【解决方案1】:

    一般来说,当您希望 Widget 包含其他 Widgets 时,您应该使用 Layout Widget。简单的Widget 不支持size_hintpos_hint,因此简单Widget 的子级通常以默认大小(100,100) 和默认位置(0,0) 结束。

    所以,改变是一个好的开始:

    class someclass(Widget):
    

    类似于:

    class Someclass(FloatLayout):
    

    请注意,类名以大写字母开头。虽然它不会在您的示例中造成任何困难,但当您使用 kv 并且您的类名以小写开头时,它可能会产生错误。

    同样,ScrollView 的孩子通常也是Layout。一种可能是GridLayout,像这样:

        GridLayout:
            size_hint_y: None
            height: self.minimum_height
            cols: 1 
            spacing: 10
            id: pofchild
    

    这里的键属性是size_hint_y: Noneheight: self.minimum_height。它们允许GridLayout 随着更多子项的添加而增长,其高度将被计算为包含子项所需的最小高度。

    然后,你可以像这样添加孩子:

    self.ids.pofchild.add_widget(Label(text=logmsg, pos=(10, self.x), size_hint_y=None, height=50))
    

    由于我们期望 GridLayout 计算其最小高度,因此我们必须为其子级提供明确的 height,因此 size_hint_y=None, height=50

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      相关资源
      最近更新 更多