You can center a Tkinter window by calculating the screen's center coordinates and setting the window to appear there using the geometry
method. Here's an example:
main.py504 chars20 lines
In this example, we first create the window
using tk.Tk()
, then set its size to 300x200 using geometry
. Next, we calculate the center coordinates of the screen using winfo_screenwidth
and winfo_screenheight
, and divide the values by 2 to get the midpoint of the screen. We then subtract half the window's height and width from the midpoint to get the top-left corner of the window. Finally, we use the geometry
method again to set the window to appear at this location, using the +x+y
syntax to specify the offset from the top-left corner of the screen.
Running this code will display a window that is centered on the screen.
gistlibby LogSnag