1,105,242 Community Members

Center a Tkinter Window

Member Avatar
sneekula
Nearly a Posting Maven
2,496 posts since Oct 2006
Reputation Points: 917 [?]
Q&As Helped to Solve: 263 [?]
Skill Endorsements: 5 [?]
 
0
 

How can I best center a Tkinter window on my display screen?

Question Self-Answered as of 7 Years Ago
Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
0
 

Not knowing Tkinter that well, here is a somewhat simple minded approach:

from Tkinter import *

def center_window(w=300, h=200):
    # get screen width and height
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    # calculate position x, y
    x = (ws/2) - (w/2)    
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))

root = Tk() 
center_window(500, 300) 
root.mainloop()
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article