【问题标题】:How to create a dynamic list of labels from a frequently updated list in Kivy如何从 Kivy 中经常更新的列表中创建动态标签列表
【发布时间】:2021-03-19 21:55:00
【问题描述】:

我正在尝试构建一个结构,该结构将包含多列数据,这些数据将被更新并且必须一起滚动。

整个应用程序有多个页面,等等。它的标签构建部分看起来确实有效,因为它将“hello world”放在页面的右侧部分。但是,即使我使用的是“索引”属性,它似乎也将所有数据都填充在彼此之上。以下是 py 和 kv 文件中的一些 sn-ps:

class RV(FloatLayout):  
    def re_build(self):
        label_list = [Label(text = "Label {}".format(x)) for x in range(20)]
        for count, x in enumerate(label_list):
            self.add_widget(Label(text='Hello World'), index = count)'
        


class MainScreen(Screen):  
    pass

class ScreenManagement(ScreenManager):
    pass 

class TestApp(App):
    def build(self):
        return ScreenManagement()


if __name__ == '__main__': 
      
    # Here the class MyApp is initialized 
    # and its run() method called. 
    TestApp().run() 

<ScreenManagement>:
    padding: 10
    MainScreen:
        id: main_screen
        name: 'main_screen'

<MainScreen>:
    GridLayout:
        rows:3
        cols:1


        Button:
            size_hint: 1, None
            height: 50
            text: "just setting up"
            on_press:
                root.ids.rv.re_build()

        
        RV:

            id: rv

        

        GridLayout:
            rows:3
            cols:2
            size_hint : None, None
            CheckBox:
                active:False
            Label:
                text: 'item 1'
            CheckBox:
                active:False
            Label:
                text: 'item 2'
            CheckBox:
                active:False
            Label:
                text: 'item 3'

任何帮助表示赞赏

【问题讨论】:

    标签: dynamic kivy label


    【解决方案1】:

    您的代码:

    self.add_widget(Label(text='Hello World'), index = count)
    

    正在将Label 添加到您的RV 类中,即FloatLayout。在FloatLayout 中,add_widget() 调用中的indexLabel 的位置没有影响,它只影响绘制标签的顺序。尝试使用不同的Layout 作为RV 类的基类。将根据index 定位其孩子的一些示例是GridLayoutBoxLayoutStackLayout

    【讨论】:

      猜你喜欢
      • 2018-05-08
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      相关资源
      最近更新 更多