Kivy-显示Python变量并在变量更改后自动更新的标签

问题描述 投票:-1回答:1

我一直在寻找这个问题的答案,无济于事。

我有一个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
python python-3.x kivy python-3.7 kivy-language
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.