我刚刚开始使用Kivy。我正在制作一个使用多个屏幕的应用程序。通过界面上部的按钮选择屏幕。每个屏幕代表程序的不同功能。现在问题来了: 我创建了一个可以更改屏幕1上的Label文本的函数。该函数似乎可以正常工作,但是当按下“ Change Label”按钮时Label文本不会更改。可能是什么问题?我一直在寻找类似的答案,但是无关,一切都应该在AFAIK上正常工作。代码如下:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
class Display(BoxLayout):
pass
class Screen_One(Screen):
mytext = "Initial Text"
def change_text(self,input):
self.mytext = input
class Screen_Two(Screen):
pass
class Screen_Three(Screen):
pass
class MyNewApp(App):
def build(self):
return Display()
if __name__ == '__main__':
MyNewApp().run()
.kv文件
<Display>:
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint: 1, None
height: '48dp'
Button:
text: 'One'
on_release: sm.current = 'screen_one'
Button:
text: 'Two'
on_release: sm.current = 'screen_two'
Button:
text: 'Three'
on_release: sm.current = 'screen_three'
ScreenManager:
id: sm
Screen_One:
Screen_Two:
Screen_Three:
<Screen_One>:
name: 'screen_one'
BoxLayout:
orientation: "vertical"
Button:
text: 'Change Label'
on_press:root.change_text("New Text")
Button:
text: 'For Later'
BoxLayout:
orientation: "vertical"
Label:
text: root.mytext
<Screen_Two>:
name: 'screen_two'
Button:
text: 'Two'
<Screen_Three>:
name: 'screen_three'
Button:
text: "Three"
谢谢您的回答