0% found this document useful (0 votes)
131 views

Python Tkinter Button

Python Tkinter Button allows users to interact with GUI applications by performing actions when clicked. It can be created and displayed in a parent window by using the Button() constructor. Common button options include text, background color, font, command function, and state. Examples demonstrate creating a basic button, adding a click function, and customizing the button's appearance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Python Tkinter Button

Python Tkinter Button allows users to interact with GUI applications by performing actions when clicked. It can be created and displayed in a parent window by using the Button() constructor. Common button options include text, background color, font, command function, and state. Examples demonstrate creating a basic button, adding a click function, and customizing the button's appearance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

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
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

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 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

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.

Python Tkinter

⊩ Python Tkinter Tutorial

⊩ Tkinter Button

⊩ Tkinter Canvas

⊩ Tkinter Checkbutton

⊩ Tkinter Entry

⊩ Tkinter Frame

⊩ Tkinter Label

⊩ Tkinter Listbox

⊩ Tkinter Message

⊩ Tkinter Radiobutton

You might also like