Python 3 – Tkinter Button
In this article, we will learn about the Tkinter button in Python. A button is a clickable widget that performs an action when it is clicked. Tkinter is a standard Python library that provides a graphical user interface (GUI) toolkit. It offers several tools, including buttons, to help build user interfaces for Python applications.
Creating a Button in Tkinter
To create a button in Tkinter, we can use the Button
class. Here is the syntax to create a button in Tkinter:
button = Button(root, text="Click Me", command=some_function)
button.pack()
In the above code, we have created a button with the text “Click Me” that calls the some_function
function. The button.pack()
function adds the button to the root window, which is the main window of the application.
Let’s create a program that creates a button and displays it on the window. First, let’s import the Tkinter library and create a root window.
from tkinter import *
root = Tk()
root.geometry("200x200")
Next, we will create a button with the text “Click Me”. When the button is clicked, it will display a message.
def display_message():
print("Hello, World!")
button = Button(root, text="Click Me", command=display_message)
button.pack()
In the above code, we have defined the display_message
function that prints “Hello, World!” to the console. We have also created a button with the Button
class and assigned the display_message
function to the command
parameter. Finally, we have used the pack()
function to add the button to the root window.
Changing Button Text and Properties
We can change the text and other properties of a button using its attributes. Here are some of the commonly used attributes of the Button
class:
text
: The text displayed on the button.width
: The width of the button in characters.height
: The height of the button in characters.activebackground
: The background color of the button when it is clicked.activeforeground
: The foreground color of the button when it is clicked.state
: The state of the button (normal, disabled, or active).
Here is an example that demonstrates how to change the text and some of the properties of a button:
button = Button(root, text="Click Me", width=20, height=5, activebackground="blue", activeforeground="white")
button.pack()
def change_text():
button["text"] = "I have been clicked!"
button2 = Button(root, text="Change Text", command=change_text)
button2.pack()
In the above code, we have created a button with a width of 20 characters and a height of 5 characters. We have also set the activebackground
to blue and the activeforeground
to white. We have defined a change_text
function that changes the text of the button when it is clicked. Finally, we have created another button that calls the change_text
function when it is clicked.
Button Styles
In Tkinter, we can change the appearance of the button by applying styles to it. A style is a set of properties that define the appearance of a widget. Here is an example that demonstrates how to apply styles to a button:
from tkinter.ttk import *
style = Style()
style.configure("TButton", font=("Arial", 12, "bold"), foreground="white", background="blue")
button = Button(root, text="Click Me", style="TButton")
button.pack()
In the above code, we have created a style for the TButton
widget class that sets the font to Arial with a font size of 12, the font weight to bold, and the foreground color to white and the background color to blue. We have then created a button with the Button
class and assigned the TButton
style to it.
Conclusion
The Tkinter button is a powerful tool that enables us to create interactive GUIs for our Python applications. We can customize the appearance and behavior of the button to suit our needs by changing its properties and styles. With the knowledge gained from this article, you can now go ahead and start building your own Python applications with buttons that can perform actions when clicked.