How to remember tkinter window position in python 3?

Tkinter is a popular GUI (Graphical User Interface) toolkit for Python that provides developers with a set of tools and widgets to create desktop applications. When designing user-friendly applications, it is crucial to consider the user's preferences and provide a seamless experience. One aspect of this is remembering the position of the application window. By saving and restoring the window position, you can ensure that the application always opens in the desired location.

To remember the window position in Tkinter, we need to store the coordinates of the window when it is closed and retrieve them when the application is launched again. We will use a configuration file to store the window position data using Python's pickle module.

Step 1: Import Required Modules

First, we need to import the necessary modules. In addition to Tkinter, we will also need the os module to handle file operations and the pickle module for object serialization ?

import tkinter as tk
import os
import pickle

Step 2: Create Functions to Save and Retrieve Window Position

Next, we will define two functions: save_window_position() and retrieve_window_position(). The save_window_position() function will save the current window position to a configuration file, while the retrieve_window_position() function will retrieve the previously saved position from the file ?

def save_window_position(window, config_file):
    position = window.winfo_geometry()
    with open(config_file, 'wb') as file:
        pickle.dump(position, file)

def retrieve_window_position(window, config_file):
    if os.path.exists(config_file):
        with open(config_file, 'rb') as file:
            position = pickle.load(file)
            window.geometry(position)

Step 3: Create the Tkinter Window and Handle Closing Event

Now, we can create the Tkinter window and handle the closing event. When the window is closed, we will call the save_window_position() function to save the current position to the configuration file ?

def on_closing():
    save_window_position(root, 'window_position.config')
    root.destroy()

root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", on_closing)

In this code snippet, we define the on_closing() function, which will be called when the window is closed. Inside this function, we call the save_window_position() function, passing the root window and the name of the configuration file as arguments. The protocol() method is used to bind the closing event to the on_closing() function.

Step 4: Retrieve the Window Position at Startup

To retrieve the previously saved window position when the application is launched again, we need to call the retrieve_window_position() function before the window is displayed ?

retrieve_window_position(root, 'window_position.config')
root.mainloop()

Complete Example

Let's put all the code together to see the complete working example ?

import tkinter as tk
import os
import pickle

def save_window_position(window, config_file):
    # Retrieve the current window position as a string
    position = window.winfo_geometry()
    
    # Open the configuration file in write binary mode
    with open(config_file, 'wb') as file:
        # Serialize and save the window position to the file
        pickle.dump(position, file)

def retrieve_window_position(window, config_file):
    # Check if the configuration file exists
    if os.path.exists(config_file):
        # Open the configuration file in read binary mode
        with open(config_file, 'rb') as file:
            # Deserialize and retrieve the previously saved window position
            position = pickle.load(file)
            
            # Set the retrieved window position for the current window
            window.geometry(position)

def on_closing():
    # Save the window position when the application is closed
    save_window_position(root, 'window_position.config')
    
    # Destroy the root window
    root.destroy()

# Create the Tkinter root window
root = tk.Tk()
root.title("Remembering Tkinter Window Position")
root.geometry("700x250")

# Add some content to make the window more interactive
label = tk.Label(root, text="Move this window around and close it.\nWhen you run the program again, it will remember the position!", 
                font=("Arial", 14), pady=20)
label.pack()

# Bind the closing event to the on_closing() function
root.protocol("WM_DELETE_WINDOW", on_closing)

# Retrieve the previous window position at startup
retrieve_window_position(root, 'window_position.config')

# Start the Tkinter event loop
root.mainloop()

The output of this program is a Tkinter window that remembers its position. When you move the window and close it, the position is saved to a configuration file. The next time you run the program, the window will open in the same position.

How It Works

The winfo_geometry() method returns a string containing the window's geometry in the format "widthxheight+x+y" (e.g., "700x250+100+50"). This string is serialized using pickle.dump() and saved to a binary file. When the application starts, pickle.load() deserializes the string and geometry() restores the window position.

Conclusion

By using a configuration file with pickle serialization, you can easily save and restore Tkinter window positions. This enhances user experience by maintaining window placement across application sessions, creating more personalized and user-friendly desktop applications.

Updated on: 2026-03-27T16:08:40+05:30

926 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements