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.
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.
Option | Description | Example |
activebackground | Background color when the button is under the cursor. | Tkinter Button activebackground |
activeforeground | Foreground color when the button is under the cursor. | Tkinter Button activeforeground |
anchor | Position of Text in Button. | Tkinter Button anchor |
bd | Border width in pixels. Default is 2. | Tkinter Button bd |
bg | Normal background color. | Tkinter Button bg |
command | Function or method to be called when the button is clicked. | Tkinter Button command |
fg | Normal foreground (text) color. | Tkinter Button fg |
font | Text font to be used for the button’s label. | Tkinter Button font |
height | Height of the button in text lines (for textual buttons) or pixels (for images). | Tkinter Button height |
highlightcolor | The color of the focus highlight when the widget has focus. | Tkinter Button highlightcolor |
image | Image to be displayed on the button (instead of text). | Tkinter Button image |
justify | How to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify. | Tkinter Button justify |
padx | Additional padding left and right of the text. | Tkinter Button padx |
pady | Additional padding above and below the text. | Tkinter Button pady |
relief | Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE. | Tkinter Button relief |
state | Set 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 |
underline | Default 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 |
width | Width of the button in letters (if displaying text) or pixels (if displaying an image). | Tkinter Button width |
wraplength | If 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
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
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.