Python Tkinter Button
Interactive controls are an important part of any graphical user interface. In Python Tkinter, the Button widget provides a simple way to give users an action they can perform by clicking a visible control. Buttons can be used for submitting forms, opening dialogs, displaying messages, starting calculations, closing windows, and performing many other tasks.
The Button widget is not limited to displaying a piece of text. Tkinter provides several configuration options that can be used to modify its appearance, dimensions, colors, alignment, border style, and state. A button can also display an image and can be connected to a Python function through the command option.
When the user clicks a button that has a command assigned to it, Tkinter executes the associated function. This event-driven behavior makes the Button widget particularly useful for building interactive desktop applications.
Table of Contents

Syntax
The general syntax for creating a Button widget is:
W = Button(parent, options)
Here, parent specifies the Tkinter window or Frame that will contain the button. The options parameter represents the different configuration properties available for controlling the button’s appearance and behavior.
For example, you can use these options to change the background color, text color, font, size, border style, padding, state, and the action performed when the button is clicked.
Tkinter Button Options
Tkinter provides a range of options for customizing buttons. The following table explains the commonly used Button configuration options.
| SN | Option | Description |
|---|---|---|
| 1 | activebackground | Specifies the background color displayed when the pointer is over the active button. |
| 2 | activeforeground | Defines the text color used when the button is active. |
| 3 | bd | Controls the width of the button border in pixels. |
| 4 | bg | Sets the normal background color of the button. |
| 5 | command | Associates a Python function with the button so it can be executed when the button is clicked. |
| 6 | fg | Specifies the foreground or text color displayed on the button. |
| 7 | font | Defines the font style and size used for the button text. |
| 8 | height | Controls the button height. For text, it is measured in lines, while image-based content can use pixel dimensions. |
| 9 | highlightcolor | Sets the color of the focus highlight around the button. |
| 10 | image | Allows an image to be displayed on the button. |
| 11 | justify | Controls the alignment of button text using LEFT, RIGHT, or CENTER. |
| 12 | padx | Defines the amount of horizontal padding inside the button. |
| 13 | pady | Defines the amount of vertical padding inside the button. |
| 14 | relief | Controls the button’s border appearance, including styles such as SUNKEN, RAISED, GROOVE, and RIDGE. |
| 15 | state | Controls whether the button is available for interaction, including states such as DISABLED and ACTIVE. |
| 16 | underline | Specifies a character position in the button text to underline. |
| 17 | width | Defines the horizontal size of the button in characters or pixels depending on its content. |
| 18 | wraplength | Causes long button text to wrap when it reaches the specified length. |
Example 1: Creating a Simple Button
The easiest way to understand the Button widget is to begin with a basic button containing a text label. The following program creates a Tkinter window and places one button inside it.
from tkinter import *
top = Tk()
top.geometry("200x100")
b = Button(top, text="Simple")
b.pack()
top.mainloop()
Output
After running the program, a small Tkinter window appears with a button labeled Simple. The button is positioned inside the window using the pack() geometry manager.
This example creates a button without assigning any command to it. Therefore, clicking the button does not perform an additional operation, but it demonstrates the basic process of creating and displaying a Tkinter Button.
Example 2: Creating Multiple Buttons with Different Attributes
The Button widget becomes more useful when different controls are given their own properties and actions. The following example creates four buttons and positions them on different sides of the window.
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("250x150")
def fun():
messagebox.showinfo("Hello", "Red Button clicked")
b1 = Button(
top,
text="Red",
command=fun,
activeforeground="red",
activebackground="pink",
pady=10
)
b2 = Button(
top,
text="Blue",
activeforeground="blue",
activebackground="pink",
pady=10
)
b3 = Button(
top,
text="Green",
activeforeground="green",
activebackground="pink",
pady=10
)
b4 = Button(
top,
text="Yellow",
activeforeground="yellow",
activebackground="pink",
pady=10
)
b1.pack(side=LEFT)
b2.pack(side=RIGHT)
b3.pack(side=TOP)
b4.pack(side=BOTTOM)
top.mainloop()
How the Example Works
The program creates four Button widgets named b1, b2, b3, and b4. Each button has a different label and active foreground color.
The Red button is connected to the fun() function through the command option. When the user clicks this button, the function calls messagebox.showinfo() and displays a message.
The pack() method is used with different side values so that the four buttons are arranged on the left, right, top, and bottom portions of the window.
Output
The resulting window contains four buttons labeled Red, Blue, Green, and Yellow. Each button is positioned on a different side of the application window.
When the Red button is selected, the assigned function runs and a message box appears with the message Red Button clicked.
The other buttons demonstrate how different visual properties can be assigned without attaching a command to them.
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
YT:- DecodeIT
Frequently Asked Questions
1. What is the Button widget in Tkinter?
The Button widget is a graphical control that allows users to trigger an action by clicking it. It is commonly used for commands, form submission, calculations, and other application operations.
2. How do I create a Button in Python Tkinter?
A Button can be created using Button(parent, options). The parent specifies where the button will be placed, while the options customize its properties.
3. What is the purpose of the command option?
The command option connects the button with a Python function. That function is executed when the user clicks the button.
4. Can a Tkinter Button display an image?
Yes. The image option can be used when an image needs to be displayed on a Button.
5. How can I disable a Tkinter Button?
The state option can be used to control button availability. Setting the state appropriately can prevent users from interacting with the button.
6. How can I change a button’s color?
The bg option can be used for the normal background color, while fg controls the text color. The activebackground and activeforeground options can change the colors when the button is active.
7. How can I change the size of a Tkinter Button?
The width and height options can be used to control the button dimensions.
Conclusion
The Python Tkinter Button widget is an essential component for creating interactive desktop applications. It gives users a direct way to trigger functions and commands through a graphical interface.
With options such as bg, fg, font, width, height, relief, and state, developers can customize the appearance and behavior of buttons according to their application requirements.
The command option is particularly important because it connects a button with application logic. By combining Buttons with functions, message boxes, Frames, Labels, Entry widgets, and other Tkinter components, developers can create interactive and user-friendly Python GUI applications.
Keywords: Python Tkinter Button, tkinter button command with arguments, how to make a button in python without tkinter, tkinter button styles, tkinter button documentation, tkinter button position, tkinter button example, tkinter button relief, python button command, python tkinter, tkinter button, python tkinter button list, python tkinter button command, python tkinter button color, python tkinter button disable, python tkinter button multiple commands, python tkinter button size, python tkinter buttons side by side, python tkinter button style,tkinter button command with arguments
how to make a button in python without tkinter
tkinter button styles
tkinter button documentation
tkinter button position
tkinter button example
tkinter button relief
python button command
python tkinter
tkinter button
python tkinter button list
python tkinter button example
python tkinter button
python tkinter button command
python tkinter button color
python tkinter button disable
python tkinter button multiple commands
python tkinter button size
python tkinter buttons side by side
python tkinter button style
python tkinter button example