We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,548 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Center a Tkinter Window

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

2
Contributors
1
Reply
12 Hours
Discussion Span
6 Years Ago
Last Updated
5
Views
Question
Answered
sneekula
Nearly a Posting Maven
2,483 posts since Oct 2006
Reputation Points: 1,000
Solved Threads: 231
Skill Endorsements: 2
Question Self-Answered as of 6 Years Ago

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()
Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0520 seconds using 2.64MB