How to Set and Manage Window Size in Python Tkinter?

In this tutorial, I will explain how to set and manage window size in Python Tkinter. As a developer working on various projects for clients across the USA, I encountered a scenario where I needed to set the window size in the Tkinter application as a part of my project, and this made me research more about this topic. I will share my findings with examples and screenshots.

Set and Manage Window Size in Python Tkinter

When creating a Tkinter window, you can set its initial size using the geometry method. The geometry method takes a string argument in the format "width×height" to specify the window’s dimensions. Here’s an example:

import tkinter as tk

window = tk.Tk()
window.geometry("800x600")  # Set the window size to 800 pixels wide and 600 pixels tall
window.mainloop()

You can see the output in the screenshot below.

Set and Manage Window Size in Python Tkinter

In this example, we create a Tkinter window named window and set its initial size to 800 pixels wide and 600 pixels tall using the geometry method.

Read Expense Tracking Application Using Python Tkinter

1. Specify Minimum and Maximum Window Sizes

Sometimes, you may want to restrict the minimum or maximum size of your Tkinter window. This can be achieved using the minsize and maxsize methods. These methods take the width and height as arguments to define the minimum and maximum window sizes, respectively. Here’s an example:

import tkinter as tk

window = tk.Tk()
window.minsize(500, 400)  # Set the minimum window size to 500 pixels wide and 400 pixels tall
window.maxsize(1000, 800)  # Set the maximum window size to 1000 pixels wide and 800 pixels tall
window.mainloop()

You can see the output in the screenshot below.

How to Set and Manage Window Size in Python Tkinter

In this code snippet, we set the minimum window size to 500 pixels wide and 400 pixels tall using the minsize method. Similarly, we set the maximum window size to 1000 pixels wide and 800 pixels tall using the maxsize method. The user will not be able to resize the window beyond these specified limits.

Check out Expense Tracking Application Using Python Tkinter

2. Handle Window Resizing

When a user resizes the Tkinter window, you may want to update the layout of your widgets accordingly. Tkinter provides the bind method to handle window resizing events. You can bind a function to the <Configure> event, which is triggered whenever the window is resized. Here’s an example:

import tkinter as tk

def on_resize(event):
    print(f"Window resized to: {event.width}x{event.height}")

window = tk.Tk()
window.bind("<Configure>", on_resize)
window.mainloop()

You can see the output in the screenshot below.

Set and Manage Window Size in Python Tkinter window resize

In this example, we define a function called on_resize that takes an event parameter. This function will be called whenever the window is resized. Inside the function, we print the new window dimensions using event.width and event.height. We bind the on_resize function to the <Configure> event using the bind method.

Read How to Generate Payslip using Python Tkinter

3. Create Responsive Layouts

To create responsive layouts that adapt to different window sizes, you can utilize Tkinter’s geometry managers, such as pack, grid, and place. These geometry managers allow you to arrange widgets within the window and control their behavior when the window is resized.

Use the Pack Geometry Manager

The pack geometry manager is a simple and flexible way to arrange widgets in a Tkinter window. It automatically adjusts the size and position of widgets based on the available space. Here’s an example:

import tkinter as tk

window = tk.Tk()

label1 = tk.Label(window, text="Label 1", bg="pink")
label1.pack(fill=tk.BOTH, expand=True)

label2 = tk.Label(window, text="Label 2", bg="skyblue")
label2.pack(fill=tk.BOTH, expand=True)

window.mainloop()

You can see the output in the screenshot below.

Set and Manage Window Size in Python Tkinter pack

In this code, we create two labels, label1 and label2, and pack them into the window using the pack method. The fill parameter is set to tk.BOTH, which means the labels will expand to fill both horizontally and vertically. The expand parameter is set to True, allow the labels to occupy any available space.

When the window is resized, the labels will automatically adjust their size to fill the available space evenly.

Check out How to convert Python file to exe using Pyinstaller

Use the Grid Geometry Manager

The grid Geometry Manager allows you to arrange widgets in a grid-like structure. It provides more precise control over the layout compared to the pack geometry manager. Here’s an example:

import tkinter as tk

window = tk.Tk()

label1 = tk.Label(window, text="Label 1", bg="red")
label1.grid(row=0, column=0, sticky=tk.NSEW)

label2 = tk.Label(window, text="Label 2", bg="blue")
label2.grid(row=0, column=1, sticky=tk.NSEW)

label3 = tk.Label(window, text="Label 3", bg="green")
label3.grid(row=1, column=0, columnspan=2, sticky=tk.NSEW)

window.grid_rowconfigure(0, weight=1)
window.grid_rowconfigure(1, weight=1)
window.grid_columnconfigure(0, weight=1)
window.grid_columnconfigure(1, weight=1)

window.mainloop()

You can see the output in the screenshot below.

Set and Manage Window Size in Python Tkinter grid

In this example, we create three labels and arrange them using the grid geometry manager. The row and column parameters specify the position of each label in the grid. The sticky parameter is set to tk.NSEW, which means the labels will stick to all four sides of their grid cell.

Check out Create Word Document in Python Tkinter

Handle Different Screen Resolutions

When designing a Tkinter application, it’s important to consider different screen resolutions to ensure a consistent user experience. You can use the winfo_screenwidth and winfo_screenheight methods to retrieve the screen dimensions and adjust your window size accordingly. Here’s an example:

import tkinter as tk

window = tk.Tk()

screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()

window_width = int(screen_width * 0.8)  # Set the window width to 80% of the screen width
window_height = int(screen_height * 0.8)  # Set the window height to 80% of the screen height

window.geometry(f"{window_width}x{window_height}")
window.mainloop()

In this code snippet, we retrieve the screen width and height using winfo_screenwidth and winfo_screenheight methods. We then calculate the window dimensions as a percentage of the screen dimensions. In this example, we set the window width and height to 80% of the screen width and height, respectively.

Read Python Tkinter Multiple Windows Tutorial

Conclusion

In this tutorial, I have explained how to set and manage window size in Python Tkinter. I discussed how to specify minimum and maximum window sizes, handle window resizing, create responsive layouts by using pack and grid geometry manager, and handle different screen resolutions.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.