gpt4 book ai didi

python - Kivy - 如何在不同的屏幕上更改 StringProperty 值?

转载 作者:太空宇宙 更新时间:2023-11-04 00:28:09 25 4
gpt4 key购买 nike

我的应用程序从数据库中获取数据并将其存储到 Python 变量中。下面的代码是一个简化版本,其中有两个屏幕。第一个屏幕有两个按钮,第二个屏幕有一个标签和一个后退按钮。第二个屏幕上的标签文本将根据按下的按钮而改变。

运行时,标签设置为 StringProperty 的值,即“Test”。单击其中一个按钮时,将运行 ChangeScreen 函数并计算出正确的新标签。第二个运行的 LabelUpdater 函数应该更改字符串属性但没有。我该如何解决这个问题?谢谢<3

python :

import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty

class DemoScreen1(Screen):
def ChangeScreen(self, button_text):
if button_text == "Button 1":
new_label = "This is the new label when button 1 is pressed"
DemoScreen2.LabelUpdater(new_label)
else:
new_label2 = "This is the new label when button 2 is pressed"
DemoScreen2.LabelUpdater(new_label2)
self.parent.current = "demoscreen2"

class DemoScreen2(Screen):
screen2_label = StringProperty("Test")
def LabelUpdater(NEW_LABEL):
screen2_label = StringProperty(NEW_LABEL)

class AppScreenManager(ScreenManager):
pass
class Tester(App):
pass
if __name__ == '__main__':
Tester().run()

基维:

AppScreenManager:
DemoScreen1:
DemoScreen2:

<DemoScreen1>:
name: "demoscreen1"
orientation: "vertical"
GridLayout:
rows: 2
Button:
id: Button1
text: "Button 1"
on_release: root.ChangeScreen(Button1.text)
Button:
id: Button2
text: "Button 2"
on_release: root.ChangeScreen(Button2.text)

<DemoScreen2>:
name: "demoscreen2"
orientation: "vertical"
GridLayout:
rows:2
Label:
text: root.screen2_label
Button:
text:"Back"
on_release: app.root.current = "demoscreen1"

最佳答案

使用 ids 并通过 AppScreenManager 也就是 ScreenManager 进行引用。

self.parent.ids.screen2.screen2_label = new_label

请参阅下面的完整示例。

import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty

class DemoScreen1(Screen):
def ChangeScreen(self, button_text):
if button_text == "Button 1":
new_label = "This is the new label when button 1 is pressed"
print('Palim')
#DemoScreen2.LabelUpdater(new_label)
self.parent.ids.screen2.screen2_label = new_label
else:
new_label2 = "This is the new label when button 2 is pressed"
self.parent.ids.screen2.screen2_label = new_label2
#DemoScreen2.LabelUpdater(new_label2)
self.parent.current = "demoscreen2"

class DemoScreen2(Screen):
screen2_label = StringProperty("Test")
def LabelUpdater(NEW_LABEL):
screen2_label = StringProperty(NEW_LABEL)

class AppScreenManager(ScreenManager):
pass
class Tester(App):
pass
if __name__ == '__main__':
Tester().run()

千伏

AppScreenManager:
DemoScreen1:
id: screen1
DemoScreen2:
id: screen2

<DemoScreen1>:
name: "demoscreen1"
orientation: "vertical"
GridLayout:
rows: 2
Button:
id: Button1
text: "Button 1"
on_release: root.ChangeScreen(Button1.text)
Button:
id: Button2
text: "Button 2"
on_release: root.ChangeScreen(Button2.text)

<DemoScreen2>:
name: "demoscreen2"
orientation: "vertical"
GridLayout:
rows:2
Label:
text: root.screen2_label
Button:
text:"Back"
on_release: app.root.current = "demoscreen1"

关于python - Kivy - 如何在不同的屏幕上更改 StringProperty 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46735007/

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