Python Tkinter Button
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.
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.
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
command Function or method to be called when the button is clicked. Tkinter Button
command
font Text font to be used for the button’s label. Tkinter Button
font
Option
height Description
Height of the button in text lines (for textual buttons) or Example
Tkinter Button
pixels (for images). height
highlightcolor The color of the focus highlight when the widget has focus. Tkinter Button
highlightcolor
justify How to show multiple text lines: LEFT to left-justify each Tkinter Button
line; CENTER to center them; or RIGHT to right-justify. 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 Tkinter Button
are SUNKEN, RAISED, GROOVE, and RIDGE. relief
state Set this option to DISABLED to gray out the button and Tkinter Button
make it unresponsive. Has the value ACTIVE when the state
mouse is over it. Default is NORMAL.
underline Default is -1, meaning that no character of the text on the Tkinter Button
button will be underlined. If nonnegative, the corresponding underline
text character will be underlined.
width W idth of the button in letters (if displaying text) or pixels (if Tkinter Button
displaying an image). width
wraplength If this value is set to a positive number, the text lines will Tkinter Button
be wrapped to fit within this length. wraplength
Python Program
import tkinter
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
def printMessage() :
print('You clicked Submit button!')
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.
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.
import tkinter
import tkinter.font as font
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.
Python Tkinter
⊩ Tkinter Button
⊩ Tkinter Canvas
⊩ Tkinter Checkbutton
⊩ Tkinter Entry
⊩ Tkinter Frame
⊩ Tkinter Label
⊩ Tkinter Listbox
⊩ Tkinter Message
⊩ Tkinter Radiobutton