第二个屏幕上有一些按钮。如果其中之一被按下-屏幕将更改,而另一屏幕上将出现一个标签,该标签上应包含按下按钮上的文字。没有错误显示,而标签根本没有任何文本。
Python:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.lang import Builder
from kivy.uix.button import Button
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
def on_pre_enter(self, *args):
btn = Button(text = "word is here", on_release =self.pressedFunction)
self.ids.container.add_widget(btn)
btn1 = Button(text = "another word is here", on_release =self.pressedFunction)
self.ids.container.add_widget(btn1)
def pressedFunction(self, instance, *args):
self.manager.current= "three"
screenThree = ScreenThree()
text = str(instance.text)
screenThree.changing_label(text)
class ScreenThree(Screen):
def changing_label(self, text):
self.ids.my_label.text = text
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("example.kv")
class MainApp(App):
def build(self):
return presentation
if __name__ == "__main__":
MainApp().run()
基维:
ScreenManagement:
ScreenOne:
ScreenTwo:
ScreenThree:
<ScreenOne>:
BoxLayout:
Button:
text: "press me"
on_release: app.root.current = "two"
<ScreenTwo>:
name: "two"
BoxLayout:
id: container
<ScreenThree>:
name: "three"
BoxLayout:
id: labelContainer
Label:
text: ""
id: my_label
我也尝试过在此处打印puttin:
class ScreenThree(Screen):
def changing_label(self, text):
self.ids.my_label.text = text
print(self.ids.my_label.text)
它显示了我期望的正确值,但是标签上仍然没有任何内容。
答案 0 :(得分:0)
my_label.text
的切换是在切换屏幕self.manager.current = "three"
后完成的。
ScreenThree 实例化了两次。第一个实例是由ScreenThree:
在kv文件中创建的(相当于Python代码中的ScreenThree()
)。第二个实例是用Python代码screenThree = ScreenThree()
创建的。
my_label.text
的填充位于第二个实例/对象中,而不是第一个实例中。因此,ScreenThree是空白/黑色窗口,因为应用程序按照kv文件(即ScreenThree的第一个实例)使用视图。
如果添加id()
功能,它将在屏幕上显示不同的存储位置。
def pressedFunction(self, instance, *args):
self.manager.current = "three"
self.debug()
screenThree = ScreenThree()
print("screenThree={0}, id(screenThree)={1}".format(screenThree, id(screenThree)))
self.debug()
text = str(instance.text)
screenThree.changing_label(text)
def debug(self):
print("\ndebug:")
print("\tself.manager.screen_names=", self.manager.screen_names)
print("\tself.manager.screens=", self.manager.screens)
for x in self.manager.screens:
print("\t\tscreen={0}, id(screen)={1}".format(x, id(x)))
id: screen_two
下添加ScreenTwo:
。这将用于引用ScreenTwo中的类属性/方法。app.root.current = "two"
替换root.manager.current = "two"
,因为默认情况下每个屏幕都具有属性manager
from kivy.properties import StringProperty
text = StringProperty('')
类中声明StringProperty,因此不需要传递参数和封装。text = str(instance.text)
替换为self.text = str(instance.text)
changing_label()
方法重命名为on_pre_enter()
方法,然后从参数列表中删除text
。self.ids.my_label.text = text' with
self.ids.my_label.text = self.manager.ids.screen_two.text` return presentation
替换为return Builder.load_file("example.kv")
,然后删除presentation = Builder.load_file("example.kv")
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.properties import StringProperty
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
text = StringProperty('')
def on_pre_enter(self, *args):
print("\nScreenTwo.on_pre_enter:")
btn = Button(text = "word is here", on_release =self.pressedFunction)
self.ids.container.add_widget(btn)
btn1 = Button(text = "another word is here", on_release =self.pressedFunction)
self.ids.container.add_widget(btn1)
def pressedFunction(self, instance, *args):
self.text = str(instance.text) # populate before switching screen
self.manager.current = "three" # switch screen
class ScreenThree(Screen):
def on_pre_enter(self, *args):
self.ids.my_label.text = self.manager.ids.screen_two.text
class ScreenManagement(ScreenManager):
pass
class MainApp(App):
def build(self):
return Builder.load_file("example.kv")
if __name__ == "__main__":
MainApp().run()
#:kivy 1.11.0
ScreenManagement:
ScreenOne:
ScreenTwo:
id: screen_two
ScreenThree:
<ScreenOne>:
BoxLayout:
Button:
text: "press me"
on_release: root.manager.current = "two" # every screen has a default property manager
<ScreenTwo>:
name: "two"
BoxLayout:
id: container
<ScreenThree>:
name: "three"
BoxLayout:
id: labelContainer
Label:
id: my_label
text: ""