gpt4 book ai didi

python - 如何使用 kivy screenmanager 引用其他屏幕中的对象

转载 作者:行者123 更新时间:2023-12-01 03:25:47 25 4
gpt4 key购买 nike

我正在尝试更新另一个屏幕中存在的字段,但没有成功。当有人能告诉我我在这里做错了什么时,我会非常非常高兴。

myscreenskv.py:

style = r'''
# File: myscreenskv.py
#: import myscreens myscreens

<ScreenManagement>:
MainScreen:
Screen1:
Screen2:

<MainScreen>:
name: 'main'
mainlog:mainlog
id: scrmain
BoxLayout:
orientation: "vertical"
Label:
text: "Main"
Label:
id: mainlog
Button:
text: "go to screen 1"
on_press:
app.root.current = "screen1"
root.action1()
Button:
text: "go to screen 2"
on_press:
app.root.current = "screen2"
root.action2()

<Screen1>:
name: 'screen1'
sc1log:sc1log
id: scr1
BoxLayout:
orientation: "vertical"
Label:
text: "Screen1"
Label:
id: sc1log
Button:
text: "go to main screen"
on_press: app.root.current = "main"
Button:
text: "go to screen 2"
on_press: app.root.current = "screen2"

<Screen2>:
name: 'screen2'
id: scr2
sc2log:sc2log
BoxLayout:
orientation: "vertical"
Label:
text: "Screen2"
Label:
id: sc2log
Button:
text: "go to main screen"
on_press: app.root.current = "main"
Button:
text: "go to screen 1"
on_press: app.root.current = "screen1"

'''

.py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from myscreenskv import style

class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)

def action1(self):
self.ids.scr1.sc1log.text = 'Coming from main'

def action2(self):
self.ids.scr2.sc2log.text = 'Coming from main'

class Screen1(Screen):
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)

def action1(self):
self.ids.main.mainlog.text = 'Coming from screen1'

def action2(self):
self.ids.scr2.sc2log.text = 'Coming from screen1'


class Screen2(Screen):
def __init__(self, **kwargs):
super(Screen2, self).__init__(**kwargs)

def action1(self):
self.ids.main.mainlog.text = 'Coming from screen2'

def action2(self):
self.ids.scr1.sc1log.text = 'Coming from screen2'

class MyscreensApp(App):
def build(self):
Builder.load_string(style)
sm = ScreenManager()
sm.add_widget(MainScreen())
sm.add_widget(Screen1())
sm.add_widget(Screen2())
sm.current = 'main'
return sm


if __name__ == '__main__':
MyscreensApp().run()

最佳答案

您正在尝试访问 ids 字典,这很好,但在完全不同的实例中,这就是此错误的原因:

AttributeError: 'super' object has no attribute '__getattr__'

您需要访问正确的实例,才能访问其属性,在您的情况下,您需要访问 ScreenManager,才能访问其 screens 属性(实例列表),您可以从中对示例文本进行所需的编辑:

MainScreen.action1():

self.manager.screens[1].sc1log.text = 'Coming from main'
# no ids, because you put it into a variable before

要了解它的工作原理,让我们看看小部件树:

<MainScreen>:
id: scrmain
BoxLayout:
Label:
Label:
id: mainlog
Button:
Button:

这里的 idsMainScreen 中的字典,可从 MainScreen().ids(实例)访问,这是输出:

{'mainlog': <WeakProxy to <kivy.uix.label.Label object at 0x12345678>>}

这意味着你不能真正将根小部件分配给它自己的字典 - 至少不是这样+无论如何都没有意义,因为你可以只调用 root ,它会给你实例根小部件。

关于python - 如何使用 kivy screenmanager 引用其他屏幕中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41407011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com