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. In this article, we will explore how to remember the Tkinter window position in Python 3.

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. There are different approaches to achieve this functionality, but we will focus on using a configuration file to store the window position data.

Let's dive into the steps to implement window position memory in a Tkinter application −

Step 1: Import the 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()

By calling retrieve_window_position() and passing the root window and the configuration file name as arguments, we can ensure that the window position is restored before the window is displayed.

Example

Let's put all the code together to see the complete 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")

# 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()

In this complete example, we import the necessary modules, define the save_window_position() and retrieve_window_position() functions, handle the closing event with on_closing(), and create the Tkinter window. We also call retrieve_window_position() before the window is displayed and save the window position when it is closed.

Output

By running this code, the Tkinter window position will be saved to the configuration file when the application is closed and retrieved when it is launched again, allowing the window to open in the same position as before.

Conclusion

In conclusion, by following the steps outlined in this article, we have learned how to remember the Tkinter window position in Python 3. By using a configuration file and the functions save_window_position() and retrieve_window_position(), we can easily save and restore the window position. This feature enhances the user experience by ensuring that the application opens in the desired location each time.

By implementing this functionality, developers can create more user-friendly and personalized Tkinter applications that remember and restore the window position according to the user's preferences. With the ability to save and retrieve the window position, users can enjoy a consistent and convenient experience when using Tkinter applications.

Updated on: 05-Dec-2023

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements