How to update information in the grid in Tkinter?

When working with Tkinter applications that use the Grid Geometry Manager, you often need to update widget properties dynamically. This can be accomplished using the configure(**options) method. The key is to assign your widgets to variables during creation, giving you global access to modify their properties later.

Basic Widget Configuration

Here's how to create a widget and update its properties −

import tkinter as tk

# Create the main window
root = tk.Tk()
root.geometry("700x350")
root.title("Grid Information Update Example")

# Create a label widget and assign to variable
label = tk.Label(root, text="This is Houston Control. We've detected a new Mission", 
                 background="skyblue", font=("Arial", 11))
label.grid(row=0, column=0, padx=10, pady=10)

# Function to update label properties
def update_label():
    label.configure(text="Mission Status: UPDATED", 
                   background="lightgreen", 
                   font=("Arial", 12, "bold"))

# Add button to trigger update
update_button = tk.Button(root, text="Update Information", command=update_label)
update_button.grid(row=1, column=0, pady=10)

root.mainloop()

Dynamic Grid Updates

You can also update grid positioning and add new widgets dynamically −

import tkinter as tk

root = tk.Tk()
root.geometry("800x400")
root.title("Dynamic Grid Updates")

# Initial widgets
status_label = tk.Label(root, text="System Status: IDLE", background="yellow")
status_label.grid(row=0, column=0, columnspan=2, pady=5)

info_label = tk.Label(root, text="No active missions", background="lightgray")
info_label.grid(row=1, column=0, pady=5)

def start_mission():
    # Update existing widgets
    status_label.configure(text="System Status: ACTIVE", background="lightgreen")
    info_label.configure(text="Mission Apollo-X initiated")
    
    # Add new dynamic widget
    progress_label = tk.Label(root, text="Progress: 0%", background="orange")
    progress_label.grid(row=2, column=0, pady=5)

def reset_system():
    # Reset all widgets to initial state
    status_label.configure(text="System Status: IDLE", background="yellow")
    info_label.configure(text="No active missions")
    
    # Remove dynamic widgets by destroying them
    for widget in root.grid_slaves(row=2):
        widget.destroy()

# Control buttons
start_btn = tk.Button(root, text="Start Mission", command=start_mission)
start_btn.grid(row=3, column=0, padx=5, pady=10)

reset_btn = tk.Button(root, text="Reset System", command=reset_system)
reset_btn.grid(row=3, column=1, padx=5, pady=10)

root.mainloop()

Key Configuration Options

Common widget properties you can update using configure()

import tkinter as tk

root = tk.Tk()
root.geometry("600x300")

# Create a demonstration label
demo_label = tk.Label(root, text="Original Text", background="white", 
                     foreground="black", font=("Arial", 10))
demo_label.grid(row=0, column=0, columnspan=3, pady=20)

# Functions to demonstrate different updates
def change_text():
    demo_label.configure(text="Text Updated!")

def change_colors():
    demo_label.configure(background="navy", foreground="white")

def change_font():
    demo_label.configure(font=("Times", 14, "italic"))

def reset_label():
    demo_label.configure(text="Original Text", background="white", 
                        foreground="black", font=("Arial", 10))

# Buttons for different updates
tk.Button(root, text="Change Text", command=change_text).grid(row=1, column=0, padx=5)
tk.Button(root, text="Change Colors", command=change_colors).grid(row=1, column=1, padx=5)
tk.Button(root, text="Change Font", command=change_font).grid(row=1, column=2, padx=5)
tk.Button(root, text="Reset", command=reset_label).grid(row=2, column=1, pady=10)

root.mainloop()

Best Practices

When updating grid information, keep these points in mind:

  • Variable Assignment: Always assign widgets to variables if you plan to modify them later
  • Configure Method: Use widget.configure(option=value) to update properties
  • Grid Management: Use grid_forget() to temporarily hide widgets or destroy() to remove them permanently
  • Dynamic Layout: Consider using grid_slaves() to find and manage widgets in specific grid positions

Conclusion

Updating grid information in Tkinter requires assigning widgets to variables and using the configure() method. This approach allows you to dynamically modify widget properties, create responsive interfaces, and manage grid layouts effectively during runtime.

Updated on: 2026-03-25T23:31:06+05:30

766 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements