【问题标题】:Kivy - Label that displays Python variable and automatically updates when variable is changedKivy - 显示 Python 变量并在变量更改时自动更新的标签
【发布时间】:2019-12-13 16:15:24
【问题描述】:

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

我有一个 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】:

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

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

    您的代码正在运行:
    .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
    

    【讨论】:

    • 我需要它来立即显示变量,无需输入。这必须从变量中加载值,因为在我的实际应用程序中,无法提前知道该值是什么。
    • 也许我解释得不好...我会做一个更好的例子并编辑此评论以添加指向 Github Gist 的链接。留意。
    • 无法编辑我的最后一条评论,所以... Here you go.
    • 我可以回答你的问题,但你也必须更新你的代码。编辑您的问题并用要点中的代码替换您的代码。否则,阅读它的人会感到非常困惑... :o)
    • @Offbox 您可以将正在运行的应用程序保存在如下变量中:self.app = App.get_running_app()。之后,您可以通过 self.app.my_var = "Whatever" 更改变量
    猜你喜欢
    • 2011-08-10
    • 1970-01-01
    • 2015-11-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多