python - Kivy: change layout when I change orientation -
i want create changes general layout when orientation, or size, of window changed. in below example, trying 3 buttons. fails, , complains buttons have parent when try add them layout. why have parent? never added them adaptwidget... more importantly, how should achieve want achieve? code below:
from kivy.app import app kivy.uix.widget import widget kivy.uix.button import button kivy.uix.boxlayout import boxlayout class adaptwidget(boxlayoutwrite failed: broken pipe def __init__(self, **kw): super(adaptwidget, self).__init__(**kw) self.but1 = button(text='but1') self.but2 = button(text='but2') self.but3 = button(text='but3') self.layout = none def on_size(self, *args): self.clear_widgets() if self.size[0] > self.size[1]: self.layout = boxlayout(orientation='horizontal') self.but1.size_hint = 0.7, 1 self.layout.add_widget(self.but1) vert = boxlayout(orientation='vertical') self.layout.add_widget(vert) self.but2.size_hint = (1,0.5) self.but3.size_hint = (1,0.5) vert.add_widget(self.but2) vert.add_widget(self.but3) else: self.layout = boxlayout(orientation='vertical') self.but1.size_hint = 1, 0.7 self.layout.add_widget(self.but1) horiz = boxlayout(oreintation='horizontal') self.layout.add_widget(horiz) self.but2.size_hint = 0.5, 1 self.but3.size_hint = 0.5, 1 horiz.add_widget(self.but2) horiz.add_widget(self.but3) self.add_widget(self.layout) class testlayoutapp(app): def build(self): return adaptwidget() if __name__ == '__main__': testlayoutapp().run()
every time size of adaptwidget
changes, on_size
callback runs - callback doesn't remove widgets parents, on_size
crash second time called. can ensure widget removed parent:
if widget.parent not none: widget.parent.remove_widget(widget)
Comments
Post a Comment