【发布时间】:2020-04-14 23:41:11
【问题描述】:
逻辑:
我按下窗口上的一个按钮,打开第一个弹出窗口;
我在第一个 Popup 上按下一个按钮,打开第二个 Popup;
我按下第二个 Popup 上的按钮,调用窗口类中的函数,更改第一个 Popup 上的标签文本。
这里是示例代码:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
Builder.load_string("""
<FirstScreen>:
BoxLayout:
Button:
text: 'open the first Popup'
on_release: root.open_first_popup()
Button:
text: 'Quit'
<FirstPopup>:
first_label: first_label
BoxLayout:
orientation: 'vertical'
Label:
id: first_label
text: 'text on first Popup'
Button:
text: 'open second Popup'
on_release: app.access_first_screen.open_second_popup()
<SecondPopup>:
BoxLayout:
orientation: 'vertical'
Label:
id: second_label
text: 'text on second Popup'
Button:
text: 'change label text on first Popup'
on_release: app.access_first_screen.change_label_text_on_first_popup()
Button:
text: 'close me to check if label text on first Popup has changed'
on_release: root.close_me()
""")
class FirstPopup(Popup):
pass
class SecondPopup(Popup):
def __init__(self, **kwargs):
super(SecondPopup, self).__init__(**kwargs)
def close_me(self):
self.dismiss()
class FirstScreen(Screen):
def open_first_popup(self):
the_first_popup = FirstPopup()
the_first_popup.open()
def open_second_popup(self):
the_second_popup = SecondPopup()
the_second_popup.open()
def change_label_text_on_first_popup(self):
app = App.get_running_app()
app.access_first_screen.the_first_popup.first_label.text = 'change to me'
class Root(ScreenManager):
def __init__(self, **kwargs):
super(Root, self).__init__(**kwargs)
xx = FirstScreen(name='first screen')
self.add_widget(xx)
class TestApp(App):
access_first_screen = FirstScreen()
access_first_popup = FirstPopup()
def build(self):
return Root()
if __name__ == '__main__':
TestApp().run()
现在的错误是:
AttributeError: 'FirstScreen' 对象没有属性 'the_first_popup'
然后我尝试了:
def change_label_text_on_first_popup(self): app = App.get_running_app() app.access_first_popup.first_label.text = 'change to me'
没有错误,但第一个 Popup 上的 Label 文本没有改变。 然后我尝试了:
def change_label_text_on_first_popup(self): app = App.get_running_app() app.ids.first_label.text = 'change to me'
错误是: AttributeError: 'TestApp' 对象没有属性 'ids'
然后我尝试了:
def change_label_text_on_first_popup(self): app = App.get_running_app() self.the_first_popup.first_label.text = 'change to me'
错误是: AttributeError:“FirstScreen”对象没有属性“the_first_popup”
我无法让它工作。
【问题讨论】:
-
在 Python 脚本中定义 ObjectProperty 类型的类属性,例如the_first_popup = ObjectProperty(None), and the_second_popup = ObjectProperty(None)。
-
嗨@ikolim,我在FirstScreen类中定义了
the_first_popup = ObjectProperty(None),然后在def change_label_text_on_first_popup(self):中尝试了一些方法,错误是AttributeError: 'NoneType' object has no attribute 'first_label',仍然无法使其工作。