How To Dynamically Resize Button Text in Tkinter?

Tkinter buttons with static text can look unprofessional when the window is resized. This tutorial shows how to make button text dynamically resize based on the window dimensions using the <Configure> event and font configuration.

Understanding Dynamic Text Resizing

Dynamic text resizing requires three key components ?

  • Grid weight configuration − Makes buttons expand with the window
  • Configure event binding − Detects window size changes
  • Font scaling function − Adjusts text size based on dimensions

Basic Dynamic Button Example

Here's a complete example that resizes button text based on window width ?

import tkinter as tk

def resize_text(event):
    # Calculate font size based on window width
    font_size = max(10, int(event.width / 15))
    button.config(font=("Arial", font_size))

# Create main window
root = tk.Tk()
root.geometry("400x200")
root.title("Dynamic Button Text")

# Configure grid weights for expansion
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

# Create button
button = tk.Button(root, text="Resize Me!", bg="lightblue")
button.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)

# Bind resize event to root window
root.bind("<Configure>", resize_text)

root.mainloop()

Advanced Multi-Button Layout

For multiple buttons with different scaling behaviors ?

import tkinter as tk

class DynamicButtonApp:
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("600x400")
        self.root.title("Multiple Dynamic Buttons")
        
        # Configure grid weights
        for i in range(2):
            self.root.grid_rowconfigure(i, weight=1)
            self.root.grid_columnconfigure(i, weight=1)
        
        # Create buttons
        self.btn1 = tk.Button(self.root, text="Button 1", bg="lightcoral")
        self.btn2 = tk.Button(self.root, text="Button 2", bg="lightgreen")
        self.btn3 = tk.Button(self.root, text="Button 3", bg="lightblue")
        self.btn4 = tk.Button(self.root, text="Button 4", bg="lightyellow")
        
        # Place buttons in grid
        self.btn1.grid(row=0, column=0, sticky="nsew", padx=5, pady=5)
        self.btn2.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
        self.btn3.grid(row=1, column=0, sticky="nsew", padx=5, pady=5)
        self.btn4.grid(row=1, column=1, sticky="nsew", padx=5, pady=5)
        
        # Bind resize event
        self.root.bind("<Configure>", self.resize_all_buttons)
    
    def resize_all_buttons(self, event):
        # Calculate font size based on window dimensions
        width_factor = event.width / 25
        height_factor = event.height / 20
        font_size = max(8, min(int(min(width_factor, height_factor)), 24))
        
        # Apply to all buttons
        for button in [self.btn1, self.btn2, self.btn3, self.btn4]:
            button.config(font=("Arial", font_size, "bold"))
    
    def run(self):
        self.root.mainloop()

# Run the application
app = DynamicButtonApp()
app.run()

Proportional Font Scaling

For more sophisticated scaling that considers both width and height ?

import tkinter as tk

def smart_resize(event):
    # Calculate scaling factors
    width_scale = event.width / 400  # Base width
    height_scale = event.height / 200  # Base height
    
    # Use the smaller scale to maintain proportions
    scale = min(width_scale, height_scale)
    font_size = max(12, int(16 * scale))
    
    # Update button font
    smart_button.config(font=("Helvetica", font_size, "bold"))

root = tk.Tk()
root.geometry("400x200")
root.minsize(200, 100)  # Set minimum window size

# Configure grid
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

# Create button with initial styling
smart_button = tk.Button(
    root, 
    text="Smart Resize", 
    bg="orange",
    fg="white",
    relief="raised",
    bd=3
)
smart_button.grid(row=0, column=0, sticky="nsew", padx=20, pady=20)

# Bind the smart resize function
root.bind("<Configure>", smart_resize)

root.mainloop()

Key Implementation Points

Component Purpose Method
Grid Weight Button expansion grid_rowconfigure(weight=1)
Event Binding Detect resize bind("<Configure>", function)
Font Scaling Text adjustment config(font=(family, size))
Sticky Option Fill available space sticky="nsew"

Conclusion

Dynamic button text resizing improves UI responsiveness by automatically adjusting font sizes based on window dimensions. Use proportional scaling and minimum size limits for the best user experience.

---
Updated on: 2026-03-25T16:53:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements