簡體   English   中英

Kivy - 顯示 Python 變量並在變量更改時自動更新的標簽

[英]Kivy - Label that displays Python variable and automatically updates when variable is changed

我一直試圖找到這個問題的答案,但無濟於事。

我有一個需要顯示全局 Python 變量的 Label 對象。 但是,它們最初僅顯示 Kivy 文件中設置的文本,除非按下按鈕,否則不會更改。 我需要標簽立即顯示變量,並在更改時自動更新。

下面是一個例子:

"""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

解決這個問題的一種方法是使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM