In this video I’ll show you how to change the background and text color of Labels with Kivy and Python.

Changing the background color and text color of a Kivy Label is a little more complicated than changing the color of other widgets. We need to set a Canvas and create a rectangle first.

We’ll also look at making text bold and italic, as well as giving it a shadow background!

Python Code: label_color.py
GitHub Code: label_color.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('label_color.kv')
class MyLayout(Widget):
	pass
	
class AwesomeApp(App):
	def build(self):
		return MyLayout()


if __name__ == '__main__':
	AwesomeApp().run()

Kivy Design Code: label_color.kv
GitHub Code: label_color.kv

<Button>
	font_size: 32
	background_normal: ''
	background_color: (0,0,1,1)

<TextInput>
	background_color: (150/255,150/255,150/255,1)	



<MyLayout>
	BoxLayout:
		orientation: "vertical"
		size: root.width, root.height
		padding:10
		spacing:10

		Label:
			text: "Name"
			font_size: 45
			background_color: (182/255, 66/255, 245/255, 1)
			canvas.before:
				Color:
					rgba: self.background_color
				Rectangle:
					size: self.size
					pos: self.pos
			# Text Properties
			color: (0,1,0,1)
			bold: True
			italic: True
			outline_color: (0,0,1)
			outline_width: 10

		TextInput:
			multiline: False

	
		Button:
			text: "Clear"
			background_color: (1,0,0,1)

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

1 comment

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

John Elder