【发布时间】:2019-11-14 17:22:21
【问题描述】:
好的,现在我正在编写一个程序。它有多个通过 ScreenManager 控制的屏幕。根屏幕(加载程序时显示的屏幕)有一个标签,其文本根据外部文件的上下文而有所不同。最初,我打算使用这样的东西:
if __name__ == "__main__":
with open(*file*) as f:
data = eval(f.read())
App.run()
并且,在所述根屏幕内部:
def on_pre_enter(self):
if data == *something*:
self.ids.*widget_id*.text = *something else*
它适用于除根屏幕以外的任何其他屏幕。我做了一些研究并了解到由于 event_dispatch 的工作方式,给定的函数(以及 on_enter)在根屏幕上不起作用。那么,有什么办法可以解决它,或者我能做些什么吗?
编辑:最小的可重现示例。
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.label import Label
class Manager(ScreenManager):
pass
class WindowOne(Screen):
def on_pre_enter(self):
self.ids.label_one.text = data
class WindowTwo(Screen):
def on_pre_enter(self):
self.ids.label_two.text = data
class TestApp(App):
def build(self):
return design
data = ''
if __name__ == "__main__":
with open('design.kv') as f:
design = Builder.load_string(f.read())
with open('data.txt') as f:
data = eval(f.read())
TestApp().run()
design.kv
Manager:
WindowOne:
WindowTwo:
<WindowOne>:
name: "one"
Label:
pos_hint: {"x": 0.15, "y": 0.15}
id: label_one
font_size: 40
text: "This Is Window One"
Button:
size_hint: (0.2, 0.2)
on_release:
app.root.current = 'two'
<WindowTwo>:
name: "two"
Label:
pos_hint: {"x": 0.15, "y": 0.15}
id: label_two
font_size: 40
Button:
size_hint: (0.2, 0.2)
on_release:
app.root.current = 'one'
如果您将 WindowOne 的 on_pre_enter 更改为 on_pre_leave,代码可以正常工作,但会返回
AttributeError: 'super' object has no attribute '__getattr__'
否则。
【问题讨论】:
-
请发帖minimal reproducible example。当我使用
ScreenManager时,初始的Screen会调用它的on_pre_enter()。 -
@JohnAnderson 完成!