Switch Screenmanager Inside Layout
Solution 1:
First you need to add the screens to your screenmanager. To do that, you can do something like this in your ScreenManager class.
def__init__(self,**kwargs):
super(ScreenManagement,self).__init__(**kwargs)
self.add_widget(Init(name="init"))
self.add_widget(Menu(name="menu"))
Now the two screens will have one manager, which is ScreenManagement
So in your kv code you call the manager like this:
Button:
text:'bottomest: go to menu'
on_press:root.manager.switch_to_menu()
You can also do like this, if you dont want to make methods:
Button:text:'bottomest: go to menu'
on_press:root.manager.current = 'menu'
Also there is a problem with your code, You add the screenmanager to a boxlayout, and try to return the boxlayout. You cannot do that. You can only show one screen at a a time. So I am guessing you want a third screen here. What you should do is retunr the screen manager in your build method, and only add screens to it. I wrote an example. I dont know if its something like this you want.
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_string("""
<Init>:
name: 'init'
BoxLayout:
orientation:'vertical'
Button:
text:'go to menu'
on_press:root.manager.current = "menu"
<Menu>:
name: 'menu'
BoxLayout:
orientation:'vertical'
Button:
text:'go to init'
on_press:root.manager.current = "init"
<ClassAllScreen>:
BoxLayout:
orientation:'vertical'
Button:
text:'go to init'
on_press:root.manager.current = "init"
Button:
text:'go to menu'
on_press:root.manager.current = "menu"
""")
classInit(Screen):
passclassMenu(Screen):
passclassClassAllScreen(Screen):
passclassClassApp(App):
defbuild(self):
sm = ScreenManager()
sm.add_widget(ClassAllScreen(name="main"))
sm.add_widget(Init(name="init"))
sm.add_widget(Menu(name="menu"))
return sm
if __name__ == '__main__':
ClassApp().run()
Solution 2:
With the great help of @EL3PHANTEN and this, I fixed my code and I guess I know where was my mistake.
I can't use app.root.sm.current='whateverScreenName'
inside kv file. I can't explain why, but I guess it has something with the moment of ScreenManagement's instatiation. As I thought from the begin, the python part is very straightforward.
Again: thanks for your help, @EL3PHANTEN :)
Here is the result:
ScreenManager inside a BoxLayout to have a Toolbar
Here is the working fixed python code:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.clock import Clock
#just solving my weak GPU issuefrom kivy import Config
Config.set('graphics', 'multisamples', '0')
kivy.require('1.9.1')
classScreenManagement(ScreenManager):
passclassInit(Screen):
passclassMenu(Screen):
passclassClassAllScreen(BoxLayout):
passclassClassApp(App):
defbuild(self):
self.root = ClassAllScreen()
return self.root
if __name__ == '__main__':
ClassApp().run()
And the Class.kv:
#: import NoTransition kivy.uix.screenmanager.NoTransition<Init>:name:'init'BoxLayout:orientation:'vertical'padding:20spacing:10Button:text:'uppest'GridLayout:spacing:10cols:2Button:text:'upperleft'Button:text:'upperright'Button:text:'bottomleft'Button:text:'bottomright'Button:text:'bottomest:gotomenu'on_press:root.manager.current='menu'<Menu>:name:'menu'BoxLayout:orientation:'vertical'padding:20spacing:10GridLayout:spacing:10cols:2Button:text:'upperleft'Button:text:'upperright'Button:text:'bottomleft'Button:text:'bottomright'Button:text:'bottomy'Button:text:'bottomest:gotoinit'on_press:root.manager.current='init'<ScreenManagement>:transition:NoTransition()Init:Menu:<ClassAllScreen>:orientation:'vertical'ScreenManagement:BoxLayout:size_hint_y:Noneheight:60spacing:5padding:5,5,0,5canvas:Color:rgba:.1,.1,.1,1Rectangle:pos:self.possize:self.sizeButton:text:'1'size_hint_x:Nonewidth:60Button:text:'2'size_hint_x:Nonewidth:60Button:text:'3'size_hint_x:Nonewidth:60
Post a Comment for "Switch Screenmanager Inside Layout"