Search code examples
pythonpython-3.xkivypython-3.7kivy-language

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


I've been trying to find an answer to this question, to no avail.

I have a Label object which needs to display a global Python variable. However, they initially only show the text that is set in the Kivy file, and won't change unless a button is pressed. I need the Label to show the variable immediately, as well as update automatically when it's changed.

Here's an example:

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

Solution

  • One way to solve this is to use Kivy Properties.

    • Add the variables as StringProperties inside your App.
    • Set the variables to be updated every time you change the input text..
    • Set the display labels to display the variables...

    Your code, working:
    The .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()
    

    ...and the .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