如何更改 kivy 中另一个 class 的标签?
How to change label from another class in kivy?
我的应用程序有 2 个 windows。第二个 window 有一个标签,我想用第一个 window 按钮更改它的文本。但每次我得到同样的错误:
AttributeError: 'super' object has no attribute '__getattr__'
这是我的 Python 代码:
from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
class WindowManager(ScreenManager):
pass
class FirstWindow(Screen):
def change_label(self):
self.parent.ids.label_id.text = 'label is changed'
class SecondWindow(Screen):
pass
Builder.load_file('new.kv')
class FirstApp(App):
def build(self):
screen_manager = ScreenManager()
screen_manager.add_widget(FirstWindow(name = 'first'))
screen_manager.add_widget(SecondWindow(name = 'second'))
return screen_manager
if __name__ == '__main__':
FirstApp().run()
和我的 .kv
文件:
#:kivy 2.0.0
<FirstWindow>:
name: "first"
padding: 50
spacing: 250
BoxLayout:
padding: 50
spacing: 250
orientation: 'vertical'
Button:
text: 'change_label'
pos_hint: {'center_x': 0.5, 'center_y': 0.15}
on_release:
root.change_label()
Button:
text: 'second window'
pos_hint: {'center_x': 0.5, 'center_y': 0.15}
on_release:
root.manager.current = 'second'
root.manager.transition.direction = 'left'
<SecondWindow>:
name: 'second'
padding: 50
spacing: 250
label_id: label_id
BoxLayout:
padding: 50
spacing: 50
orientation: 'vertical'
Label:
id: label_id
text: 'this label'
Button:
text: 'first window'
on_release:
app.root.current = "first"
root.manager.transition.direction = 'right'
您只需要获取对 SecondWindow
实例的引用,您可以使用 ScreenManager
:
的 get_screen()
方法来实现
class FirstWindow(Screen):
def change_label(self):
# self.parent.ids.label_id.text = 'label is changed'
self.manager.get_screen('second').ids.label_id.text = 'label is changed'
我的应用程序有 2 个 windows。第二个 window 有一个标签,我想用第一个 window 按钮更改它的文本。但每次我得到同样的错误:
AttributeError: 'super' object has no attribute '__getattr__'
这是我的 Python 代码:
from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
class WindowManager(ScreenManager):
pass
class FirstWindow(Screen):
def change_label(self):
self.parent.ids.label_id.text = 'label is changed'
class SecondWindow(Screen):
pass
Builder.load_file('new.kv')
class FirstApp(App):
def build(self):
screen_manager = ScreenManager()
screen_manager.add_widget(FirstWindow(name = 'first'))
screen_manager.add_widget(SecondWindow(name = 'second'))
return screen_manager
if __name__ == '__main__':
FirstApp().run()
和我的 .kv
文件:
#:kivy 2.0.0
<FirstWindow>:
name: "first"
padding: 50
spacing: 250
BoxLayout:
padding: 50
spacing: 250
orientation: 'vertical'
Button:
text: 'change_label'
pos_hint: {'center_x': 0.5, 'center_y': 0.15}
on_release:
root.change_label()
Button:
text: 'second window'
pos_hint: {'center_x': 0.5, 'center_y': 0.15}
on_release:
root.manager.current = 'second'
root.manager.transition.direction = 'left'
<SecondWindow>:
name: 'second'
padding: 50
spacing: 250
label_id: label_id
BoxLayout:
padding: 50
spacing: 50
orientation: 'vertical'
Label:
id: label_id
text: 'this label'
Button:
text: 'first window'
on_release:
app.root.current = "first"
root.manager.transition.direction = 'right'
您只需要获取对 SecondWindow
实例的引用,您可以使用 ScreenManager
:
get_screen()
方法来实现
class FirstWindow(Screen):
def change_label(self):
# self.parent.ids.label_id.text = 'label is changed'
self.manager.get_screen('second').ids.label_id.text = 'label is changed'