Question about ScreenManager

32 views
Skip to first unread message

Frank Tischhauser

unread,
May 15, 2020, 5:10:17 AM5/15/20
to Kivy users support
Hey, I have an issue with the ScreenManager, I understand the documentation and I can change the screen in many situations, but I'd like to create an app class method: def change_screen(screen_name), that changes screen each time it's called. In order to make all the process easy to do. I tried, but it doesn't work and I don't understand why. Can you help me with this?
 
p.s (I know I could remove the method and just write on_press : manager.current = 'input_screen' in the kv string, but that's not what I want as I said before)

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen


kv = """
ScreenManager:
id: manager

HomeScreen:
name : 'home_screen'
MDLabel:
text: 'home_screen'
halign: 'center'
MDRectangleFlatButton:
text: 'go to input_screen'
pos_hint: {'center_x': .5, 'center_y': .3}
on_press: app.change_screen('input_screen')

InputScreen:
name : 'input_screen'
MDLabel:
text: 'input_screen'
halign: 'center'
MDRectangleFlatButton:
text: 'go to home_screen'
pos_hint: {'center_x': .5, 'center_y': .3}
on_press: app.change_screen('home_screen')
"""


class HomeScreen(Screen):
pass


class InputScreen(Screen):
pass



class TestApp(MDApp):

def build(self):
self.theme_cls.primary_palette = "Teal"
return Builder.load_string(kv)

def change_screen(self, screen_name):
self.root.ids.manager.current = screen_name


if __name__ == "__main__":
TestApp().run()

Elliot Garbus

unread,
May 15, 2020, 10:52:03 AM5/15/20
to kivy-...@googlegroups.com

The issue is that the screenmanager is your rootwidget, so you don’t need to go into the ids to address the screenlmanager.

 

When you have these kinds of issues you can print out the elements of the pointer you are using…

 

In app if you print(self.root) you will see it is the ScreenManager.  Therefore access current as:

    def change_screen(self, screen_name):
       
self.root.current = screen_name

Alternatively if you put the ScreenManager in a BoxLayout, then the ScreenManager is no longer the root widget and your original code should work.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/0c37062c-ff31-4b08-a9bf-f8f16b658011%40googlegroups.com.

 

Frank Tischhauser

unread,
May 16, 2020, 7:00:47 AM5/16/20
to Kivy users support
OK it works, thank you for your answer and your tips

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages