Working With Widgets

Widgets are the bread and butter of Tkinter. Each widget in Tkinter is defined by a class. Here are some of the widgets available in Tkinter:

Widget
Class
Description

Label A widget used to display text on the screen

Button A button that can contain text and can perform an action when clicked

Entry A text entry widget that allows only a single line of text

Text A text entry widget that allows multi-line text entry

Frame A rectangular region used to group related widgets or provide space between widgets

There are many more widgets than the ones listed here, that do all sorts of complicated functions. We'll take you through using the Label and Button widgets, and if you're interested you'll have the opportunity to learn some more about Tkinter. For now, lets take a closer look at Labels.

Label widgets are used to display text or images. The text displayed by a Label widget can’t be edited by the user. It’s for display purposes only. As you saw in the example at the beginning of this tutorial, you can create a Label widget by using the tk.Label class and passing a string to the text parameter:

label = tk.Label(text="Hello, Tkinter")

Label widgets display text with the default system text color and the default system text background color. These are typically black and white, respectively, but you may see different colors if you have changed these settings in your operating system.

You can control Label text and background colors using the foreground and background parameters:

label = tk.Label(

text="Hello, Tkinter",

foreground="white", # Set the text color to white

background="black" # Set the background color to black

)

There are numerous valid color names, including:

  • "red"

  • "orange"

  • "yellow"

  • "green"

  • "blue"

  • "purple"

A chart with most of the valid color names is available here. For a full reference, including macOS and Windows-specific system colors that are controlled by the current system theme, check out the colors manual page.

You can also specify a color using hexadecimal RGB values (don't worry if you don't quite understand this - hexadecimal is an advanced computing topic that we haven't covered):

label = tk.Label(text="Hello, Tkinter", background="#34A2FE")

This sets the label background to a nice, light blue color. Hexadecimal RGB values are more cryptic than named colors, but they’re also more flexible. Fortunately, there are tools available that make getting hexadecimal color codes relatively painless.

If you don’t feel like typing out foreground and background all the time, then you can use the shorthand fg and bg parameters to set the foreground and background colors:

label = tk.Label(text="Hello, Tkinter", fg="white", bg="black")

You can also control the width and height of a label with the width and height parameters:

label = tk.Label(

text="Hello, Tkinter",

fg="white",

bg="black",

width=10,

height=10

)

Here’s what this label looks like in a window:

It may seem strange that the label in the window isn’t square even though the width and height are both set to 10. This is because the width and height are measured in text units. One horizontal text unit is determined by the width of the character "0", or the number zero, in the default system font. Similarly, one vertical text unit is determined by the height of the character "0".

Labels are great for displaying text, but they don't help you get input from a user. The next widget we'll cover lets you do just that.