Tkinter Button

tkinter buttons

In Tkinter, labels are used to display information but buttons are designed for the user to interact with. Buttons are a great way for a user to provide input to your program especially now in the age of the internet when everything is just a click away. When a user clicks a button, they expect something to happen. Our job as the programmer is to code in that behavior. The programmer determines what happens when the button is clicked. This can be set up with the use of a callback function. The callback function has the behavior to be executed when a button is clicked and it can be passed as a parameter to the Button constructor when creating a new button. Of course, we can also define the look and feel of buttons. We’ll take a look at how to work with buttons in Tkinter in this tutorial.


Attaching A Button

This code will create a button, and center it in the application windows. Pretty simple.


Defining A Callback Function

The existing button does not do anything when it is clicked yet. We can change that by adding a callback function to the program. This is a two-step process.

  • Define the callback
  • Pass the callback to the button

Now this code will just print out a message to the console when the button is clicked as we can see.

Optional callback configuration: Instead of passing the callback as a command to the Button constructor, you may instead use the .config() method like so.


button.invoke()

It is possible to invoke the button as if someone had clicked it using the .invoke() method. In this snippet, we can make the program virtually click 5 times. This can be useful if you need to execute a callback method from more than one place in your code, because you only need to configure it once, and that’s when you’re defining this button.

The Button Was Clicked!
The Button Was Clicked!
The Button Was Clicked!
The Button Was Clicked!
The Button Was Clicked!

Adding an Image to a button

One way to make the button more appealing in the application is to use an image. Just like with the Label you first need to create it using the PhotoImage constructor. This constructor takes the argument of the path to python_logo.gif. With the logo object created, we can use the config method on the button to set the image property to that logo. In this example, the button is to the left of the text. Another method of the PhotoImage object that we can use to resize images within tkinter is called subsample. You pass in an X and Y and it will use Xth and every Yth pixel in each direction.

tkinter button with image

Disable A Button

Buttons have a state which determines whether they’re active and can be used, or disabled and unusable. To set the state of a widget, use the state method to modify the disabled flag.

tkinter button disabled

instate() method

To check the current state of a button, you can use the instate() method.

True
False

More Resources

Tkinter Button Summary

In this tutorial, we learned a little bit about how to use buttons with Tkinter. They provide an easy way to get input from a user of the application so that an action can be taken by way of a callback function when clicked.