Python Tkinter Button

Python Tkinter Button is a GUI element that allows user to perform an action and interact with the GUI application.

In this tutorial, we will learn how to create a Button widget and display in your GUI application.

Syntax – Tkinter Button Widget

Following is the syntax of Tkinter Button Widget.

tk.Button(parent, option=value, ...)

where tk is the main or top level window, created from Tk() class. parent is the parent window of Button, in which you would like to pack the Button.

Button() constructor returns Button widget.

ADVERTISEMENT

Tkinter Button Options

Following table presents the possible options, with description and reference to example, that you can provide to a Button() constructor to alter its behavior or appearance.

OptionDescriptionExample
activebackgroundBackground color when the button is under the cursor.Tkinter Button activebackground
activeforegroundForeground color when the button is under the cursor.Tkinter Button activeforeground
anchorPosition of Text in Button.Tkinter Button anchor
bdBorder width in pixels. Default is 2.Tkinter Button bd
bgNormal background color.Tkinter Button bg
commandFunction or method to be called when the button is clicked.Tkinter Button command
fgNormal foreground (text) color.Tkinter Button fg
fontText font to be used for the button’s label.Tkinter Button font
heightHeight of the button in text lines (for textual buttons) or pixels (for images).Tkinter Button height
highlightcolorThe color of the focus highlight when the widget has focus.Tkinter Button highlightcolor
imageImage to be displayed on the button (instead of text).Tkinter Button image
justifyHow to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify.Tkinter Button justify
padxAdditional padding left and right of the text.Tkinter Button padx
padyAdditional padding above and below the text.Tkinter Button pady
reliefRelief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.Tkinter Button relief
stateSet this option to DISABLED to gray out the button and make it unresponsive. Has the value ACTIVE when the mouse is over it. Default is NORMAL.Tkinter Button state
underlineDefault is -1, meaning that no character of the text on the button will be underlined. If nonnegative, the corresponding text character will be underlined.Tkinter Button underline
widthWidth of the button in letters (if displaying text) or pixels (if displaying an image).Tkinter Button width
wraplengthIf this value is set to a positive number, the text lines will be wrapped to fit within this length.Tkinter Button wraplength

Example 1 – Tkinter Button

In the following example, we will create a GUI application with Tkinter, create a Button widget and place it in the window.

Python Program

import tkinter

window_main = tkinter.Tk(className='Tkinter - TutorialKart', )
window_main.geometry("400x200")

button_submit = tkinter.Button(window_main, text ="Submit")

button_submit.pack()
window_main.mainloop()

Output

Python Tkinter Button

Example 2 – Tkinter Button – Click

In the following example, we will create a Tkinter GUI application with a button and a callable action when user clicks on it.

Python Program

import tkinter

window_main = tkinter.Tk(className='Tkinter - TutorialKart', )
window_main.geometry("400x200")

def printMessage() :
    print('You clicked Submit button!')

button_submit = tkinter.Button(window_main, text ="Submit", command=printMessage)

button_submit.pack()
window_main.mainloop()

Output

The callable function is called when you click on the ‘Submit’ button. As we just printing a message to the standard output in the printMessage function, you will see console output as shown below.

You clicked Submit button!

Example 3 – Tkinter Button – With Font Style, Background Color and Text Color, etc

In the following example, we will create a Tkinter GUI application with a button. We will change the font style, width, height, background color, text color.

example.py – Python Program

import tkinter
import tkinter.font as font

window_main = tkinter.Tk(className='Tkinter - TutorialKart', )
window_main.geometry("400x200")

button_font = font.Font(family='Helvitica', size=20)

button_submit = tkinter.Button(window_main,
    text="Submit",
    bg='#45b592',
    fg='#ffffff',
    bd=0,
    font=button_font,
    height=2,
    width=15)
button_submit.pack()

window_main.mainloop()

Output

Tkinter Button

Conclusion

In this Python Tutorial, we learned how to create Tkinter Button, and about the different options that a Button widget has, with examples for each of the option.