首页 > 解决方案 > Kivy - 显示 Python 变量并在变量更改时自动更新的标签

问题描述

我一直试图找到这个问题的答案,但无济于事。

我有一个 Label 对象,它需要显示一个全局 Python 变量。但是,它们最初只显示在 Kivy 文件中设置的文本,除非按下按钮,否则它们不会改变。我需要 Label 立即显示变量,并在更改时自动更新。

这是一个例子:

"""Hopefully this is a better example of what I need."""
# ============================== Import Modules ==============================

# ===== Import Kivy =====
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty

# ============================================================================


# =========================== Variable Definitions ===========================

# ===== Define ScreenManager =====
sm = ScreenManager()

# ============================================================================


# ========================== Define screen classes ===========================

class InputScreen(Screen):
    """Input stuff into text boxes."""

    var1 = StringProperty()
    var2 = StringProperty()
    var3 = StringProperty()
    # Take the input from these text boxes and show them on the next screen


class DisplayScreen(Screen):
    """Show content from previous text boxes."""

# ============================================================================


# ============================= Define main app ==============================

class MainApp(App):
    """Class for main app."""

    def build(self):
        """Build app."""
        # Add screens to ScreenManager
        sm.add_widget(InputScreen(name='input'))
        sm.add_widget(DisplayScreen(name='display'))

        # Display current screen
        return sm

# ============================================================================


# ===== Run program =====
if __name__ == '__main__':
    MainApp().run()
#:kivy 1.11.1

<InputScreen>:
    var1: var1_input.text
    var2: var2_input.text
    var3: var3_input.text
    Label:
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.85}
        text: "var1 ="
    TextInput:
        id: var1_input
        font_size: 24
        size_hint: None, None
        size: 96, 48
        pos_hint: {'center_x': 0.5, 'center_y': 0.77}
        multiline: False
    Label:
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.65}
        text: "var2 ="
    TextInput:
        id: var2_input
        font_size: 24
        size_hint: None, None
        size: 96, 48
        pos_hint: {'center_x': 0.5, 'center_y': 0.57}
        multiline: False
    Label:
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.45}
        text: "var3 ="
    TextInput:
        id: var3_input
        font_size: 24
        size_hint: None, None
        size: 96, 48
        pos_hint: {'center_x': 0.5, 'center_y': 0.37}
        multiline: False
    Button:
        font_size: 20
        size_hint: None, None
        size: 120, 50
        pos_hint: {'center_x': 0.5, 'center_y': 0.15}
        text: "Continue"
        on_press:
            root.manager.transition.direction = 'left'
            root.manager.current = 'display'

<DisplayScreen>:
    Button:
        font_size: 20
        size_hint: None, None
        size: 80, 50
        pos_hint: {'x': 0, 'top': 1}
        text: "Back"
        on_press:
            root.manager.transition.direction = 'right'
            root.manager.current = 'input'
    Label:
        id: label1
        text_to_display:"(this should show var1 from previous screen)"
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.7}
        text: "var1 = " + self.text_to_display
    Label:
        id: label2
        text_to_display:"(this should show var2 from previous screen)"
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        text: "var2 = " + self.text_to_display
    Label:
        id: label3
        text_to_display:"(this should show var3 from previous screen)"
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.3}
        text: "var3 = " + self.text_to_display

标签: pythonpython-3.xkivypython-3.7kivy-language

解决方案


解决此问题的一种方法是使用Kivy Properties.

  • 将变量添加到StringProperties您的App.
  • 每次更改输入文本时设置要更新的变量..
  • 设置显示标签以显示变量...

你的代码,工作:
.py:

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

sm = ScreenManager()


class InputScreen(Screen):
    """Input stuff into text boxes."""


class DisplayScreen(Screen):
    """Show content from previous text boxes."""


class MainApp(App):
    """Class for main app."""
    var1 = StringProperty()
    var2 = StringProperty()
    var3 = StringProperty()

    def build(self):
        """Build app."""
        # Add screens to ScreenManager
        sm.add_widget(InputScreen(name='input'))
        sm.add_widget(DisplayScreen(name='display'))

        # Display current screen
        return sm


# ===== Run program =====
if __name__ == '__main__':
    MainApp().run()

...和.kv:

<InputScreen>:
    Label:
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.85}
        text: "var1 ="
    TextInput:
        id: var1_input
        font_size: 24
        size_hint: None, None
        size: 96, 48
        pos_hint: {'center_x': 0.5, 'center_y': 0.77}
        multiline: False
        # when the text changes, write it to the variable
        on_text: app.var1 = self.text
    Label:
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.65}
        text: "var2 ="
    TextInput:
        id: var2_input
        font_size: 24
        size_hint: None, None
        size: 96, 48
        pos_hint: {'center_x': 0.5, 'center_y': 0.57}
        multiline: False
        # when the text changes, write it to the variable
        on_text: app.var2 = self.text
    Label:
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.45}
        text: "var3 ="
    TextInput:
        id: var3_input
        font_size: 24
        size_hint: None, None
        size: 96, 48
        pos_hint: {'center_x': 0.5, 'center_y': 0.37}
        multiline: False
        # when the text changes, write it to the variable
        on_text: app.var3 = self.text
    Button:
        font_size: 20
        size_hint: None, None
        size: 120, 50
        pos_hint: {'center_x': 0.5, 'center_y': 0.15}
        text: "Continue"
        on_press:
            root.manager.transition.direction = 'left'
            root.manager.current = 'display'

<DisplayScreen>:
    Button:
        font_size: 20
        size_hint: None, None
        size: 80, 50
        pos_hint: {'x': 0, 'top': 1}
        text: "Back"
        on_press:
            root.manager.transition.direction = 'right'
            root.manager.current = 'input'
    Label:
        id: label1
        text_to_display:"(this should show var1 from previous screen)"
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.7}
        text: "var1 = " + app.var1  # get the updated value
    Label:
        id: label2
        text_to_display:"(this should show var2 from previous screen)"
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        text: "var2 = " + app.var2  # get the updated value
    Label:
        id: label3
        text_to_display:"(this should show var3 from previous screen)"
        font_size: 20
        pos_hint: {'center_x': 0.5, 'center_y': 0.3}
        text: "var3 = " + app.var3  # get the updated value

推荐阅读