Python Kivy Screen Manager Wiget Scope
I am trying to control a screen manager from buttons in a separate class, but I cannot figure out what to set on the button on_press: statements. Kivy file: :
Solution 1:
Navigating widget trees has been a pain for me, and AFAIK you can't traverse the widget tree the way you'd like.
You can, however, simplify your widget tree, make sure everything shares the same root, and use ids.
Here's how I did it (I also moved everything to kv language):
kv
FloatLayout:AnchorLayout:anchor_x:'center'anchor_y:'top'Label:size_hint:1,.1text:'My App'AnchorLayout:anchor_x:'center'anchor_y:'center'ScreenManager:id:managersize_hint:1,.8Screen:name:'first'Label:text:'First screen'Screen:name:'second'Label:text:'Second screen'Screen:name:'third'Label:text:'Third screen'AnchorLayout:anchor_x:'center'anchor_y:'bottom'BoxLayout:orientation:'horizontal'size_hint:1,.1Button:text:'first'on_press:root.ids.manager.current='first'Button:text:'second'on_press:root.ids.manager.current='second'Button:text:'third'on_press:root.ids.manager.current='third'
python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.image import Image
classMyAppApp(App):
defbuild(self):
return Builder.load_file('MyApp.kv')
if __name__ == '__main__':
MyAppApp().run()
Post a Comment for "Python Kivy Screen Manager Wiget Scope"