In this video I’ll show you how to update labels in your Kivy app based on user input.
Updating labels and other things based on user input is a fundamental part of building any app. Fortunately it’s pretty easy to update a label. In this video I’ll show you how to enter text in a text box, click a button, and then update a label with whatever you typed into the button.
Python Code: update_label.py
GitHub Code: update_label.py
from kivy.app import App from kivy.uix.widget import Widget from kivy.properties import ObjectProperty from kivy.lang import Builder # Designate Our .kv design file Builder.load_file('update_label.kv') class MyLayout(Widget): def press(self): # Create variables for our widget name = self.ids.name_input.text # Update the label self.ids.name_label.text = f'Hello {name}!' # Clear input box self.ids.name_input.text = '' class AwesomeApp(App): def build(self): return MyLayout() if __name__ == '__main__': AwesomeApp().run()
Kivy Design Code: update_label.kv
GitHub Code: update_label.kv
<MyLayout> BoxLayout: orientation: "vertical" size: root.width, root.height Label: id: name_label text: "What's Your Name?" font_size: 32 TextInput: id: name_input multiline: False size_hint: (1, .5) Button: size_hint: (1, .5) font_size: 32 text: "Submit" on_press: root.press()
Add comment