简体   繁体   English

在 Kivy (Python) 中跨屏幕更新标签 - 与 ScreenManager 的.kv 和 .py 文件之间的交互(缺乏)问题

[英]Updating Labels across Screens in Kivy (Python) - Problem with (lack of) interactions between .kv and .py files with ScreenManager

I'm trying to build a Kivy application that has 2 screens which are re-used over and over again with different text.我正在尝试构建一个 Kivy 应用程序,该应用程序具有 2 个屏幕,这些屏幕通过不同的文本一遍又一遍地重复使用。

So I go from a FirstScreen with a Label that says "First1" to a SecondScreen with a Label that says "Second1", and then back to the FirstScreen but this time with the Label "First2", then SecondScreen and "Second2", and so on and so forth. So I go from a FirstScreen with a Label that says "First1" to a SecondScreen with a Label that says "Second1", and then back to the FirstScreen but this time with the Label "First2", then SecondScreen and "Second2", and等等等等。

The code for this is pretty straightforward, but there seems to be a problem in updating the Label text without a designated update button.此代码非常简单,但在没有指定更新按钮的情况下更新 Label 文本似乎存在问题。 For some reason, my Python code manages to update the text, but it isn't updated in my.kv file.出于某种原因,我的 Python 代码设法更新了文本,但它没有在 my.kv 文件中更新。 So for instance, my print statements will tell me that the Label text is "First2", but Kivy displays "First1" for me.例如,我的打印语句会告诉我 Label 文本是“First2”,但 Kivy 为我显示“First1”。 I've illustrated this in the Screenshot below:我在下面的屏幕截图中说明了这一点:

在此处输入图像描述

By adding a Button that updates the text on press, everything is updated, synced up and works, but I'd really like it to work without the extra user input.通过添加一个在按下时更新文本的按钮,所有内容都会更新、同步并正常工作,但我真的希望它能够在没有额外用户输入的情况下工作。 Does anybody know how I can go about this?有谁知道我怎么能 go 关于这个? I've scoured the docs and stackoverflow questions left and right but can't seem to find the answer to my seemingly simple problem.我已经左右搜索了文档和 stackoverflow 问题,但似乎找不到我看似简单的问题的答案。

Here's the code:这是代码:

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

S_ID = 1  # global screen ID. I'm using this to detect what text to use.


class FirstScreen(Screen):
    text = StringProperty("")
    lbl = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(FirstScreen, self).__init__(**kwargs)
        global S_ID
        print("\nS_ID is ", S_ID)
        self.update()

    def update(self):
        print("FIRST - UPDATE")
        if S_ID == 1:
            print("FIRST1")
            self.text = "FIRST1"
        elif S_ID == 2:
            print("FIRST2")
            self.text = "FIRST2"
            print("self.lbl.text", self.lbl.text)
        else:
            print("FIRST ELSE")
            self.text = "FIRST ELSE"

    def pressed(self):
        sm.current = "second"


class SecondScreen(Screen):
    text = StringProperty("")

    def __init__(self, **kwargs):
        super(SecondScreen, self).__init__(**kwargs)
        self.update()

    def update(self):
        print("SECOND - UPDATE")
        if S_ID == 1:
            print("SECOND1")
            self.text = "SECOND1"
        elif S_ID == 2:
            print("SECOND2")
            self.text = "SECOND2"
        else:
            print("SECOND ELSE")
            self.text = "SECOND ELSE"

    def pressed(self):
        global S_ID
        S_ID += 1
        FirstScreen.update(FirstScreen())
        sm.current = "first"


sm = ScreenManager()
kv = Builder.load_file("test.kv")
sm.add_widget(FirstScreen(name='first'))
sm.add_widget(SecondScreen(name='second'))

sm.current = "first"


class MyApp(App):

    def build(self):
        return sm


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

and here's the.kv file:这是.kv文件:

<FirstScreen>:
    name: "first"
    lbl: lbl:

    GridLayout:
        cols:2
        Label:
            id: lbl
            text: root.text

        Button:
            text: "next"
            on_press: root.pressed()

<SecondScreen>:
    name: "second"

    GridLayout:
        cols:2
        Label:
            text: root.text

        Button:
            text: "next"
            on_press:
                root.pressed()

The problem is your statement:问题是你的陈述:

FirstScreen.update(FirstScreen())

This statement is creating a new instance of FirstScreen and updating that instance.此语句正在创建FirstScreen的新实例并更新该实例。 Unfortunately, that instance is not the one shown in your GUI.不幸的是,该实例不是您的 GUI 中显示的实例。 You can correct that by replacing the above statement with:您可以通过将上述语句替换为:

    first_screen = self.manager.get_screen('first')
    first_screen.update()

This code gets the instance of FirstScreen from the ScreenManager and calls update() on that instance.此代码从ScreenManager获取FirstScreen的实例并在该实例上调用update()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM