Please explain how I can create an application on kivy so that each screen has separate py and kv files. Including screens that are called from screens. In fact, we need an example application of the form: Mainscreen-> 1screen-> 2screen
Here is one way to do it:
main.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
class WindowManager(ScreenManager):
pass
class TestApp(App):
def build(self):
return Builder.load_file('main.kv') # this method can be eliminated if `main.kv` is renamed to `test.kv`
if __name__ == '__main__':
TestApp().run()
main.kv:
# import the python files defining the Screens
#: import Screen1 screen1.Screen1
#: import Screen2 screen2.Screen2
# include the kv files for the other Screens
#: include screen1.kv
#: include screen2.kv
WindowManager:
Screen1:
Screen2:
screen1.py:
from kivy.uix.screenmanager import Screen
class Screen1(Screen):
pass
screen1.kv:
<Screen1>:
name: 'screen1'
BoxLayout:
Label:
text: 'Screen1'
Button:
text: 'back'
on_release: app.root.current = 'screen2'
screen2.py:
from kivy.uix.screenmanager import Screen
class Screen2(Screen):
pass
screen2.kv:
<Screen2>:
name: 'screen2'
BoxLayout:
Label:
text: 'Screen2'
Button:
text: 'back'
on_release: app.root.current = 'screen1'
Of course, there are different ways to do this. Another approach is to import all the py
files in main.py
and just use Builder.load_file()
in main.py
for each kv
file
If you put all the files (except main.py) in a subfolder named subs
, then you just need to make a change to main.py:
class TestApp(App):
def build(self):
return Builder.load_file('subs/main.kv')
And changes to main.kv
:
# import the python files defining the Screens
#: import Screen1 subs.screen1.Screen1
#: import Screen2 subs.screen2.Screen2
# include the kv files for the other Screens
#: include subs/screen1.kv
#: include subs/screen2.kv
WindowManager:
Screen1:
Screen2:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.