[英]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.