2017-07-26 16 views
0

以下のコードから、show_buttons()メソッドでレイアウトをBoxLayoutからGridLayoutに変更する予定ですが、これは起こっていないので、まだBoxLayoutが表示されています。私は説明に感謝します、ありがとう。あなたがウィジェットをクリアについて再考してちょうどScreenManagerを使用してdiffent画面に移動したい場合があります、と述べたあなたが親にのGridLayoutを追加するのを忘れPython KIvy:レイアウトが変更されないのはなぜですか?

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() 

答えて

1

...

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) # <----------------- 

関連する問題