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()

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder