Python KIvy:为什么布局没有改变?
从下面的代码中,我希望在show_buttons()方法中将布局从BoxLayout更改为GridLayout,但这并没有发生,我仍然看到BoxLayout。谢谢你的解释Python KIvy:为什么布局没有改变?,python,kivy,Python,Kivy,从下面的代码中,我希望在show_buttons()方法中将布局从BoxLayout更改为GridLayout,但这并没有发生,我仍然看到BoxLayout。谢谢你的解释 class MainScreen(BoxLayout): def show_buttons(self, button): self.clear_widgets() self.layout = GridLayout(cols=2) if button.text == 'B
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()
您忘记将网格布局添加到父级
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) # <-----------------
def显示按钮(self,按钮):
self.clear_widgets()
self.layout=GridLayout(cols=2,size\u hint=(1.0,1.0))
如果button.text==“按钮1”:
对于范围(100110)内的x:
t=('Button%s'%x]
self.add_小部件(按钮(text=t))
self.add\u小部件(self.layout)#