Back to: Python Programming
The Tkinter Button widget is a graphical control element used in Python’s Tkinter library to create clickable buttons in a graphical user interface (GUI). It provides a way for users to trigger actions or events when clicked.
# button = you click on it, it does stuff
from tkinter import *
window = Tk()
button = Button(window, text="Click me!")
button.pack()
window.mainloop()
The output window has a “Click me!” button in it, we can click it, but nothing happens because we have not told it what to do.

In order to make this button do something when clicked, we could define a function and link the button to it. We do this by using button.config(command=<function-name>) which will trigger the function to execute:
from tkinter import *
def clickme(): # <-- Our button calls this function
print("Hello!")
window = Tk()
button = Button(window, text="Click me!") # <-- displays a button with Click me! written on it
button.config(command=clickme) # <-- performs call back of function
button.pack()
window.mainloop()
Styling the Button
We can configure most aspects of the button – the following example demonstrates some options:
from tkinter import *
def clickme(): # <-- Our button calls this function
print("Hello!")
window = Tk()
button = Button(window, # <-- subsequent lines style the button
text="Click me!", # <-- text on the button
font=('Ink Free', 50, 'bold'), # <-- the fond face of text on the button
bg='green', # <-- the background colour of the button surface
fg='#0000ff', # <-- the foreground colour of the button text
activebackground='#ff6200', # <-- the on-click background colour
activeforeground='#fffb1f', # <-- the on-click foreground colour
command=clickme) # <-- performs call back of function
button.pack()
window.mainloop()
There are a couple of different ways to accomplish this button or label styling – I prefer the right hand side method:
from tkinter import *
def clickme():
print("Hello!")
window = Tk()
button = Button(window, text='Click me!!!')
button.config(command=click)
button.config(font=('Ink Free',50,'bold'))
button.config(bg='#ff6200')
button.config(fg='#fffb1f')
button.config(activebackground='#FF0000')
button.config(activeforeground='#fffb1f')
button.pack()
window.mainloop()
from tkinter import *
def clickme():
print("Hello!")
window = Tk()
button = Button(window,
text="Click me!",
font=('Ink Free', 50, 'bold'),
bg='#ff6200',
fg='#fffb1f',
activebackground='#ff0000',
activeforeground='#fffb1f',
command=clickme)
button.pack()
window.mainloop()
Tkinter Button Widget Syntax
The syntax to use the button widget is given below.
Syntax: Button(parent, options)
Parameters
- parent: This parameter is used to represents the parent window.
- options:There are many options which are available and they can be used as key-value pairs separated by commas.
Tkinter Button Options
- activebackground: Background colour when the button is under the cursor.
- activeforeground: Foreground colour when the button is under the cursor.
- anchor: Width of the border around the outside of the button
- bd or borderwidth: Width of the border around the outside of the button
- bg or background: Normal background colour.
- command: Function or method to be called when the button is clicked.
- cursor: Selects the cursor to be shown when the mouse is over the button.
- text: Text displayed on the button.
- disabledforeground: Foreground colour is used when the button is disabled.
- fg or foreground: Normal foreground (text) colour.
- font: Text font to be used for the button’s label.
- height: Height of the button in text lines
- highlightbackground: Colour of the focus highlight when the widget does not have focus.
- highlightcolor: The colour (spelled …color) of the focus highlight when the widget has focus.
- highlightthickness: Thickness of the focus highlight.
- image: Image to be displayed on the button (instead of text).
- justify: tk.LEFT to left-justify each line; tk.CENTER to center them; or tk.RIGHT to right-justify.
- overrelief: The relief style to be used while the mouse is on the button; default relief is tk.RAISED.
- padx, pady: padding left and right of the text. / padding above and below the text.
- width: Width of the button in letters (if displaying text) or pixels (if displaying an image).
- underline: Default is -1, underline=1 would underline the second character of the button’s text.
- width: Width of the button in letters
- wraplength: If this value is set to a positive number, the text lines will be wrapped to fit within this length.
Methods
- flash(): Causes the button to flash several times between active and normal colours. Leaves the button in the state it was in originally. Ignored if the button is disabled.
- invoke(): Calls the button’s command callback, and returns what that function returns. Has no effect if the button is disabled or there is no callback.
import tkinter as tk
def button_clicked():
print("Button clicked!")
window = tk.Tk()
# Creating a button with specified options
button = tk.Button(window,
text="Click Me",
command=button_clicked,
activebackground="blue",
activeforeground="white",
anchor="center",
bd=3,
bg="lightgray",
cursor="hand2",
disabledforeground="gray",
fg="black",
font=("Arial", 12),
height=2,
highlightbackground="black",
highlightcolor="green",
highlightthickness=2,
justify="center",
overrelief="raised",
padx=10,
pady=5,
width=15,
wraplength=100)
button.pack(padx=20, pady=20)
window.mainloop()