首页 > 解决方案 > Kivy如何更新小部件

问题描述

所以我有一个 ScrollView,它显示基于全局列表的按钮。看起来像这样 在此处输入图像描述

为了创建这个,我使用了这个方法

def create_scrollview(self, dt):
    global connectionList
    
    base = [i for i in connectionList]

    removeButton = ["X" for i in connectionList] 
     
    self.layout = GridLayout(cols=2, spacing=10, size_hint_y=None)

    self.layout.bind(minimum_height=self.layout.setter("height"))

    for element in base:

        self.layout.add_widget(Button(text=element, size=(50, 50),
                                 background_color = (.3, .3, .3, 1),
                                 size_hint=(1, None)))

        self.layout.add_widget(Button(text = 'X', size = (10, 10),
                                 size_hint = (.15, .1),
                                 background_normal = '',
                                 background_color = (1 , .5, .5, 1)))         
        
    self.scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
    
    self.scrollview.add_widget(self.layout)
    
    self.view.add_widget(self.scrollview)

所以基本上当我点击一个按钮时,它需要触发两个函数,一个将“四”添加到列表中,一个调用这个方法。

    def resetScrollView(self):
   
    global connectionList
    
    base = [i for i in connectionList]

    removeButton = ["X" for i in connectionList] 
  
    for element in base:

        self.layout.add_widget(Button(text=element, size=(50, 50),
                                 background_color = (.3, .3, .3, 1),
                                 size_hint=(1, None)))

        self.layout.add_widget(Button(text = 'X', size = (10, 10),
                                 size_hint = (.15, .1),
                                 background_normal = '',
                                 background_color = (1 , .5, .5, 1)))            
    

    print(connectionList)

所以基本上,它会打印更新后的 connectionList ('first','second','third','fourth') 但是 UI 没有更新并且保持不变。你们能告诉我如何使 ScrollView 镜像 connectionList(列表中每个字符串的每个按钮)

标签: pythonkivykivy-language

解决方案


推荐阅读