Last active
January 6, 2020 12:03
-
-
Save Kovak/5977673 to your computer and use it in GitHub Desktop.
Example of using a screen manager with buttons to switch between screens in kivy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.app import App | |
from kivy.uix.widget import Widget | |
from kivy.properties import StringProperty, ObjectProperty | |
from kivy.uix.screenmanager import Screen | |
class MainMenu(Screen): | |
name = StringProperty('main_menu') | |
class OtherMenu(Screen): | |
name = StringProperty('other_menu') | |
class RootWidget(Widget): | |
state = StringProperty('set_main_menu_state') | |
screen_manager = ObjectProperty(None) | |
def __init__(self, **kwargs): | |
super(RootWidget, self).__init__(**kwargs) | |
def on_state(self, instance, value): | |
if value == 'main_menu': | |
self.screen_manager.current = 'main_menu' | |
def set_state(self, state): | |
if state == 'main_menu': | |
self.screen_manager.current = 'other_menu' | |
if state == 'other_menu': | |
self.screen_manager.current = 'main_menu' | |
class TestApp(App): | |
def build(self): | |
pass | |
if __name__ == '__main__': | |
TestApp().run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RootWidget: | |
<RootWidget>: | |
screen_manager: screen_manager | |
ScreenManager: | |
id: screen_manager | |
current: 'main_menu' | |
pos: root.pos | |
size: root.size | |
MainMenu: | |
screen_manager: root | |
size: root.size | |
pos: root.pos | |
OtherMenu: | |
screen_manager: root | |
size: root.size | |
pos: root.pos | |
<MainMenu>: | |
screen_manager: self.screen_manager | |
FloatLayout: | |
Button: | |
text: 'Start' | |
size_hint: (.4, .2) | |
pos_hint: {'x': .3, 'y': .4} | |
on_press: root.screen_manager.set_state(root.name) | |
<OtherMenu>: | |
screen_manager: self.screen_manager | |
FloatLayout: | |
Button: | |
text: 'Other' | |
size_hint: (.4, .2) | |
pos_hint: {'x': .3, 'y': .4} | |
on_press: root.screen_manager.set_state(root.name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment