Displaying Clickable Buttons

Challenge 5-2: User Interfaces

Button widgets are used to display clickable buttons. They can be configured to call a function whenever they’re clicked. You can learn about how to call functions from button clicks in the extra tutorial we'll point to at the end of this section. For now, take a look at how to create and style a Button.

There are many similarities between Button and Label widgets. In many ways, a Button is just a Label that you can click! The same keyword arguments you use to create and style a Label will work with Button widgets. For example, the following code creates a Button with a blue background and yellow text. It also sets the width and height to 25 and 5 text units, respectively:

button = tk.Button(

text="Click me!",

width=25,

height=5,

bg="blue",

fg="yellow",

)

Here’s what the button looks like in a window:

Pretty nifty!

Tkinter has many more functions than what we've described here, but this challenge has been a short introduction to the fascinating world of GUI design. If you would like more information and further challenges in using Tkinter, including the opportunity to design your very own computer apps, check out the full tutorial from Real Python here!