I'm using python and kivy to write an app. For simplicity, I want to find a way to switch between screens without having to declare a function to go to a screen in every single class.
For example, in my UserLogin class, I define a function called to_mainmenu
that can be used to go back to the EntryScreen widget. I want to use that function in the MainScreen widget in the corresponding KV file, specifically for the button that says 'option', but because it was declared in another class, I don't know the syntax to call to_mainmenu
within that kv file. How can I call it?
Below is the python code:
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
kivy.require('1.11.1')
class EntryScreen(GridLayout):
def to_newuser(self):
main_app.screen_manager.current = 'New Account'
# yes
def to_userlogin(self):
main_app.screen_manager.current = 'User Login'
def to_mainmenu(self):
main_app.screen_manager.current = 'Main Screen'
class UserLogin(GridLayout):
name1 = ObjectProperty(None)
password1 = ObjectProperty(None)
def to_entryscreen(self):
main_app.screen_manager.current = 'Entry Screen'
def to_login(self):
main_app.screen_manager.current = 'Login Screen'
def to_mainscreen(self):
main_app.screen_manager.current = 'Main Screen'
class NewAccount(GridLayout):
def to_mainmenu(self):
main_app.screen_manager.current = 'Main Screen'
class MainScreen(GridLayout):
pass
class MapGem(App):
def build(self):
self.screen_manager = ScreenManager()
self.entry_screen = EntryScreen()
screen = Screen(name='Entry Screen')
screen.add_widget(self.entry_screen)
self.screen_manager.add_widget(screen)
self.user_login = UserLogin()
screen = Screen(name='User Login')
screen.add_widget(self.user_login)
self.screen_manager.add_widget(screen)
self.new_account = NewAccount()
screen = Screen(name='New Account')
screen.add_widget(self.new_account)
self.screen_manager.add_widget(screen)
self.mainscreen = MainScreen()
screen = Screen(name='Main Screen')
screen.add_widget(self.mainscreen)
self.screen_manager.add_widget(screen)
return self.screen_manager
if __name__ == '__main__':
main_app = MapGem()
main_app.run()
Below is the kivy file
<EntryScreen>
cols: 1
Button:
text: 'New User'
on_press: root.to_newuser()
Button:
text: 'Login'
on_press: root.to_userlogin()
Button:
text: 'Skip Login'
on_press:
#root.manager.transition.direction = 'right'
root.to_mainmenu()
<UserLogin>
name1: name
password1: password
cols: 2
Label:
text: 'Login to Account'
Label:
text: ''
Label:
text: 'Username'
TextInput:
id: name
multiline: False
Label:
text: 'Password'
TextInput:
id: password
multiline: False
password: True
Button:
text: 'Submit'
on_press: root.to_mainscreen()
Button:
text: 'Go back to Entry Screen'
on_press: root.to_entryscreen()
<NewAccount>
cols: 2
Label:
text: 'Create New Account'
Label:
text: ''
Label:
text: 'Username'
TextInput:
multiline: False
Label:
text: 'Password'
TextInput:
multiline: False
password: True
Button:
text: 'Submit'
on_press: root.to_mainmenu()
<MainScreen>
cols: 1
Button:
text: 'Create New Maps'
Button:
text: 'View Maps'
Button:
text: 'Options'
# on_press: something to call UserLogin.to_entryscreen()
I'm still getting used to how kivy handles OOP and what root, app, and other names mean. So, I'm sure this is a very basic and easy question, so I apologize ahead of time if it seems mundane.