Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to update a Button widget in Tkinter?
We can update a Button widget in Tkinter in various ways, for example, we can change its size, change its background color, or remove its border. The configure() method is the primary way to update button properties after creation.
Syntax
button.configure(property=value)
Common Button Properties
| Property | Description | Example |
|---|---|---|
text |
Button text | "Click Me" |
bg |
Background color | "green" |
font |
Font style and size | ("Arial", 12, "bold") |
borderwidth |
Border thickness |
0 (no border) |
state |
Button state | "disabled" |
Example
In the following example, we create three buttons that update their own properties when clicked ?
# Import the required library
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Define geometry of the window
win.geometry("700x300")
win.title("Button Update Example")
# Function to Increase the Size of the Button
def Button_Size():
button1.configure(font=('Calibri', 20, 'bold'))
# Function to change the background color
def Button_Color():
button2.configure(bg='green', fg='white')
# Function to Remove Border
def Button_Border():
button3.configure(borderwidth=0, relief='flat')
# First Button
button1 = Button(win, text="Increase the Button Size",
command=Button_Size)
button1.pack(pady=20)
# Second Button
button2 = Button(win, text="Change the Background Color",
command=Button_Color)
button2.pack(pady=20)
# Third Button
button3 = Button(win, text="Remove the Border",
command=Button_Border)
button3.pack(pady=20)
win.mainloop()
Multiple Property Updates
You can update multiple properties at once using a single configure() call ?
from tkinter import *
def update_button():
btn.configure(
text="Updated!",
bg="blue",
fg="white",
font=("Arial", 14, "bold"),
width=20,
height=2
)
root = Tk()
root.geometry("300x200")
btn = Button(root, text="Click to Update", command=update_button)
btn.pack(pady=50)
root.mainloop()
Dynamic Text Updates
You can create buttons that change their text based on user interaction ?
from tkinter import *
def toggle_text():
current_text = btn.cget('text')
if current_text == "ON":
btn.configure(text="OFF", bg="red")
else:
btn.configure(text="ON", bg="green")
root = Tk()
root.geometry("200x150")
btn = Button(root, text="OFF", bg="red", fg="white",
font=("Arial", 12), command=toggle_text)
btn.pack(pady=50)
root.mainloop()
Key Points
- Use
configure()to update any button property after creation - Multiple properties can be updated in a single
configure()call - Use
cget()to retrieve current property values - Changes take effect immediately without redrawing the entire window
Conclusion
The configure() method provides a flexible way to update Tkinter button properties dynamically. You can modify appearance, behavior, and content to create interactive user interfaces that respond to user actions.
Advertisements
