Utilities for positioning Tkinter windows on screen.
The frontend.window_utils module provides functions to center Toplevel windows on the screen. All dialogs in the Tkinter interface use this module so that windows appear in consistent positions instead of random locations.
place_window_centered(win, width=None, height=None, *, preserve_size=False, max_width=900, max_height=650, max_width_ratio=0.7, max_height_ratio=0.7) -> None
Position a Toplevel window centered on screen.
Parameters:
win: The Toplevel window to positionwidth: Desired width in pixels. IfNoneand notpreserve_size, uses computed size frommax_widthandmax_width_ratioheight: Desired height in pixels. IfNoneand notpreserve_size, uses computed size frommax_heightandmax_height_ratiopreserve_size: IfTrue, keeps the window's current size and only sets position. Call after widgets are packed/gridded and afterwin.update_idletasks()max_width: Cap for width when computed from screen (default 900)max_height: Cap for height when computed from screen (default 650)max_width_ratio: Max width as fraction of screen (0.0–1.0). Applied whenwidthisNoneor to cap explicitwidthon small screensmax_height_ratio: Max height as fraction of screen (0.0–1.0). Applied whenheightisNoneor to cap explicitheighton small screens
Usage patterns:
- Preserve natural size (most dialogs): Call after building content, use
preserve_size=True - Fixed size (help, config): Pass explicit
widthandheight; ratios cap size on small screens
Example:
from frontend.window_utils import place_window_centered
from tkinter import Toplevel, ttk
# Dialog with natural size (from layout)
dlg = Toplevel()
frame = ttk.Frame(dlg, padding=10)
frame.pack()
ttk.Label(frame, text="Select option:").pack()
ttk.Button(frame, text="OK", command=dlg.destroy).pack()
place_window_centered(dlg, preserve_size=True)
# Dialog with fixed size (e.g. help window)
help_win = Toplevel()
# ... build content ...
place_window_centered(help_win, 900, 650, max_width_ratio=0.7, max_height_ratio=0.7)