繁体   English   中英

Python KIvy:为什么布局未更改?

[英]Python KIvy: Why layout not changed?

从下面的代码中,我希望在show_buttons()方法中将布局从BoxLayout更改为GridLayout,但是这没有发生,并且我仍然看到BoxLayout。 我希望得到一个解释,谢谢。

class MainScreen(BoxLayout):

    def show_buttons(self, button):
        self.clear_widgets()
        self.layout = GridLayout(cols=2)
        if button.text == 'Button 1':
            for x in range (100, 110):
              t = ('Button %s' % x)
              self.add_widget(Button(text=t))

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.orientation='vertical'
        self.add_widget(Label(text='Select Button', size_hint = (1, 0.2)))
        self.button1=Button(text="Button 1")
        self.button1.bind(on_press=self.show_buttons)
        self.add_widget(self.button1)
        self.button2=Button(text="Button 2")
        self.button2.bind(on_press=self.show_buttons)
        self.add_widget(self.button2)             

class MyApp(App):
    def build(self): 
        return MainScreen() 

if __name__ == '__main__':
    MyApp().run()

您忘记了将GridLayout添加到父级...

def show_buttons(self, button):
    self.clear_widgets()
    self.layout = GridLayout(cols=2, size_hint=(1.0, 1.0))
    if button.text == 'Button 1':
        for x in range (100, 110):
          t = ('Button %s' % x)
          self.add_widget(Button(text=t))
    self.add_widget(self.layout) # <-----------------

就是说,您可能需要重新考虑清除窗口小部件,而只是使用ScreenManager移至其他屏幕

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM