You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kivy的两个屏幕间保持Label文本一致

Fixing Label Text Sync Between Kivy Screens

Hey there! Let's get those two labels synced up properly. The issue with your current code is that global variables don't trigger Kivy's UI update system—we need to use Kivy's StringProperty instead, which is built exactly for reactive UI state sharing between widgets.

Here's the corrected, working code:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty  # Don't forget this import!

Builder.load_string("""
<MenuScreen>:
    FloatLayout:
        Button:
            text: "Go to Settings"
            pos: 270, 240
            size_hint: .30, .10
            background_color: 0,1,0,0.75
            on_press: root.manager.current = 'settings'
        Button:
            text: "Update Text"
            pos: 270, 140
            size_hint: .30, .10
            background_color: 1,0,0,0.75
            on_press: app.shared_text = "New Synced Text!"  # Update shared property
        Label:
            id: ratio
            text: app.shared_text  # Bind label to shared property
            pos: 0,90
            font_size:30
            color:1,0,0,1
<SettingsScreen>:
    FloatLayout:
        Button:
            text: "Back to Menu"
            pos: 270, 240
            size_hint: .30, .10
            background_color: 0,1,0,0.75
            on_press: root.manager.current = 'menu'
        Label:
            id: ratio
            text: app.shared_text  # Same binding for automatic sync
            pos: 0,90
            font_size:30
            color:1,0,0,1
""")

class MenuScreen(Screen):
    pass  # No more messy global variables or extra methods needed!

class SettingsScreen(Screen):
    pass

class Quizz(App):
    shared_text = StringProperty("Initial Synced Text")  # Reactive shared property

    def build(self):
        self.title = 'Quizz'
        Window.clearcolor = (0, 1, 1, 0.25)
        sm = ScreenManager()
        sm.add_widget(MenuScreen(name='menu'))
        sm.add_widget(SettingsScreen(name='settings'))
        return sm

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

Let's break down what changed:

  • Added StringProperty import: This is key for creating reactive values that trigger UI updates.
  • Created shared_text in the App class: This property lives in your main App instance, which both screens can access via app.shared_text in the KV language.
  • Bound both labels to app.shared_text: Instead of using global variables or custom methods, we directly link each label's text attribute to the shared property. When shared_text changes, Kivy automatically updates both labels.
  • Removed unnecessary code: The texte() and texte2() methods are gone—property binding handles all the syncing work for us.

Why this works better than global variables:

Kivy's properties are observable—when their value changes, any UI element bound to them gets notified and refreshes automatically. Global variables don't have this built-in reactivity, so even if you change the variable, the UI won't update unless you manually force it (which is messy and error-prone).

How to modify the text dynamically:

You can update shared_text from anywhere in your code:

  • From a button press (like the "Update Text" button in the example)
  • From a method in MenuScreen: self.app.shared_text = "New Custom Text"
  • Directly in the App class during initialization: self.shared_text = "Starting Text"

This approach keeps your code clean, follows Kivy's best practices, and ensures your labels stay perfectly synced no matter where you update the text.

内容的提问来源于stack exchange,提问作者Eragon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:37:43