How can I tell a Tkinter window where to open, based on screen dimensions? I would like it to open in the middle.
-
[an answer](http://stackoverflow.com/a/10018670/1217270) – Honest Abe Feb 17 '13 at 03:34
7 Answers
This answer is based on Rachel's answer. Her code did not work originally, but with some tweaking I was able to fix the mistakes.
import tkinter as tk
root = tk.Tk() # create a Tk root window
w = 800 # width for the Tk root
h = 650 # height for the Tk root
# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
# set the dimensions of the screen
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.mainloop() # starts the mainloop

- 1
- 1

- 8,256
- 16
- 40
- 60
Try this
import tkinter as tk
def center_window(width=300, height=200):
# get screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# calculate position x and y coordinates
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
root = tk.Tk()
center_window(500, 400)
root.mainloop()

- 15,395
- 32
- 113
- 196

- 27,943
- 21
- 72
- 81
-
or refer to http://eurion.net/python-snippets/snippet/Center%20window.html for an alternative method – Rachel Gallen Feb 16 '13 at 13:38
-
1
-
3@RachelGallen: do you realize that code is for a completely different toolkit? You can't use pyqt to center a tkinter window. – Bryan Oakley Feb 16 '13 at 16:31
-
@BryanOakley i offered an alternative solution. besides he said he's working in python so why not? – Rachel Gallen Feb 16 '13 at 16:44
-
@RachelGallen Thanks for the help... but you should test your code before answering. Once you had given me the basic idea though, I was able to figure it out. – xxmbabanexx Feb 16 '13 at 16:49
-
@RachelGallen upvote for helping me :) But... it is really easy to [install Python 2.7.3 for Windows](http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi) Upvote granted. – xxmbabanexx Feb 16 '13 at 16:57
-
1
-
There is even better centering function in [this answer](https://stackoverflow.com/a/10018670/3015186) which takes into account the titlebar height of the window (and the window frame width). – Niko Föhr Feb 22 '19 at 11:47
root.geometry('250x150+0+0')
The first two parameters are the width and height of the window. The last two parameters are x and y screen coordinates. You can specify the required x and y coordinates

- 3,209
- 3
- 18
- 39

- 521
- 12
- 31
-
1Great general solution to the problem that allows the user more flexibility. Thanks! – Jack Duane Oct 31 '18 at 08:39
If you would like the window to be centered, this type of function may help you:
def center_window(size, window) :
window_width = size[0] #Fetches the width you gave as arg. Alternatively window.winfo_width can be used if width is not to be fixed by you.
window_height = size[1] #Fetches the height you gave as arg. Alternatively window.winfo_height can be used if height is not to be fixed by you.
window_x = int((window.winfo_screenwidth() / 2) - (window_width / 2)) #Calculates the x for the window to be in the centre
window_y = int((window.winfo_screenheight() / 2) - (window_height / 2)) #Calculates the y for the window to be in the centre
window_geometry = str(window_width) + 'x' + str(window_height) + '+' + str(window_x) + '+' + str(window_y) #Creates a geometric string argument
window.geometry(window_geometry) #Sets the geometry accordingly.
return
Here, the window.winfo_screenwidth
function is used for getting the width
of the device screen.
And the window.winfo_screenheight
function is used for getting the height
of the device screen.
Here you can call this function and pass a tuple with the (width, height) of the screen as the size.
You can customize the calculation as much as you want and it will change accordingly.

- 1,351
- 2
- 13
- 25
-
1Simple difference from answers above, but works for different individual windows, which is nice – Toon Tran Jan 31 '21 at 17:19
root.geometry('520x400+350+200')
Explanation: ('width x height + X coordinate + Y coordinate')

- 57,289
- 29
- 176
- 237

- 59
- 1
-
5What is the difference between you answer and the answer of @LordDraagon? – Ivan Olshansky Mar 22 '19 at 05:23
A slight expansion to allow positioning around the screen
def position_window(width=300, height=200, xf = 0.5, yf = 0.5):
# get screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# calculate position x and y coordinates
x = (screen_width*xf) - (width/2)
y = (screen_height*yf) - (height/2)
if x > screen_width-width:
x = screen_width-width
if x < width:
x = 0.0
if y > screen_height-height:
y = screen_height-height*1.25
if y < 0:
y = 0
root.geometry('%dx%d+%d+%d' % (width, height, x, y))

- 2,194
- 1
- 14
- 24
I prefer to use
links_conf.eval('tk::PlaceWindow . center')
It is possible to see that the window shows wherever you want and then changing its position, but hey! It's easy to use!

- 21
- 5