Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy-如何在不同的屏幕上更改StringProperty值?_Python_Kivy - Fatal编程技术网

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

Python Kivy-如何在不同的屏幕上更改StringProperty值?,python,kivy,Python,Kivy,我的应用程序从数据库中获取数据,并存储到Python中的变量中。下面的代码是一个简化版本,其中有两个屏幕。第一个屏幕有两个按钮,第二个屏幕有一个标签和一个后退按钮。第二个屏幕上的标签文本将根据按下的按钮而变化 运行时,标签设置为StringProperty的值,即“Test”。单击其中一个按钮时,ChangeScreen功能将运行并生成正确的新标签。第二个上的LabelUpdater函数正在运行,它应该更改string属性,但不会更改。如何解决此问题?感谢使用ids并通过AppScreenMan

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


运行时,标签设置为StringProperty的值,即“Test”。单击其中一个按钮时,ChangeScreen功能将运行并生成正确的新标签。第二个上的LabelUpdater函数正在运行,它应该更改string属性,但不会更改。如何解决此问题?感谢使用
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:
演示屏幕1:
id:屏幕1
演示屏幕2:
id:屏幕2
:
名称:“demoscreen1”
方向:“垂直”
网格布局:
行数:2
按钮:
id:按钮1
文本:“按钮1”
发布时:root.ChangeScreen(按钮1.text)
按钮:
id:按钮2
文本:“按钮2”
发布时:root.ChangeScreen(按钮2.text)
:
名称:“demoscreen2”
方向:“垂直”
网格布局:
行数:2
标签:
文本:root.screen2\u标签
按钮:
文本:“返回”
发布时:app.root.current=“demoscreen1”

我知道这是件很简单的事情,我尝试了所有方法,但从来都不知道这是件好事。非常感谢
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"