这是一个包含两个屏幕的简单代码:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<MenuScreen>:
BoxLayout:
Button:
text: 'ONE'
on_release: root.manager.current = 'main'
Button:
text: 'TWO'
on_release: root.manager.current = 'main'
<MainScreen>:
Button:
on_release: root.manager.current = 'menu'
""")
class MenuScreen(Screen):
pass
class MainScreen(Screen):
pass
class ScreenManager(ScreenManager):
pass
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(MainScreen(name='main'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
我需要以下内容:
如果我点击&#34; ONE&#34;按钮(在MenuScreen中),应该在MainScreen上显示一个按钮,其中包含文本&#34; one&#34; (例如)
如果我点击&#34; TWO&#34;按钮(在MenuScreen中),应该在MainScreen上显示一个带有文本&#34; 2&#34;
我该怎么做?
P.S。抱歉我的英文不好
答案 0 :(得分:0)
你的代码非常混乱;我已将其更改为更具可读性,并分为两个文件:
test.kv:
MyScreenManager:
MenuScreen:
id: menu
MainScreen:
id: main
<MenuScreen>:
name: 'menu'
BoxLayout:
Button:
text: 'ONE'
on_release: root.switch_screen('one')
Button:
text: 'TWO'
on_release: root.switch_screen('two')
<MainScreen>:
name: 'main'
Button:
id: button
on_release: root.manager.current = 'menu'
main.py:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
objects = {}
class MyScreenManager(ScreenManager):
pass
class MenuScreen(Screen):
def switch_screen(self, text):
objects['manager'].current = 'main'
objects['main_button'].text = text
class MainScreen(Screen):
pass
class TestApp(App):
def build(self):
objects['manager'] = self.root
objects['main_button'] = self.root.ids.main.ids.button
if __name__ == '__main__':
TestApp().run()