Hit enter after type your search item

Python tkinter Button: Learn with 10 Examples

Creating buttons with Python tkinter library

The tkinter is a standard Python interface for developing GUI-based applications in Python.

You may use tkinter for creating different GUI components like buttons, frames, labels, scrollbars, radio buttons, etc.
In this tutorial, we will show you creating buttons using tkinter.

Creating a simple button example

In this program, we have created a simple button by using tkinter library. See the program below with a little details that how it worked.

Python program:



How did it work?

  • First of all, the tkinter library is imported (if you are using Python 3.x). For 2.x you need to use Tkinter (i.e. T in capital).
  • We created a Tk object and specified the window that contains the button.
  • Then we specified the button option. In that case, we provided caption/text for the button.

Closing the button on click

In the following example, we used almost the same code as above except we added an action (command) as the button is clicked.

Python code:

After executing the above program, as you click on the button, it will close.

An example of various border styles in buttons

The Relief option is used to set the border style of the buttons. The following values can be used for the Relief option:

  • FLAT

See the Python program below with all these values and output:



Setting the background and foreground colors of the button

In this example, we will set the background and foreground colors of the buttons.

The bg option is used to set the background color of the button in a normal state.

The fg sets the foreground color i.e. text color of the button.

For example, bg = “Red” and fg = “White”.

See the example below where we will set the colors of the above example’s created buttons by using these two options.

Python program:



Setting the background/foreground colors on mouse click

Similarly, you may set the button background and foreground color on mouse click. For that, you may use the following two options:

  • activebackground (for button background color)
  • activeforeground (for button text/caption)

The following example shows how:



Changing the height and width of the button example

By using height and width options, you may set the button size as using tkinter library.

The following example creates a button bigger than the above examples – both for height and width.




Display a message box by using command option example

The following example shows some action as the button is clicked.

For that, we will use the command option of the button widget and call a function.

Inside the function, msgbox(), we will display a message box with simple text.

So, click the button to call the function and display the message box.




Note that, you have to import messagebox from tkinter in order to work with this example.

Styling the button by color codes

In this example, the background (bg), foreground (fg), activebackground, and activeforeground colors are set by using the color code rather than color names. For example, #034232.

Besides, we also set the title of the window containing the button unlike the above examples with the default “tk” title. Have a look:




Display buttons in the specified position in a frame/window

The following ways can be used to set the position of the button:

  • pack (to display button in the center as in all the above examples)
  • grid (specify row and column)
  • place (specify x & y positions to display button).

The example below shows the usage of these ways.

By grid option




By place option




This div height required for enabling the sticky sidebar