Python 3, Tkinter 8.6. GUI examples in Windows 10
When your first window loads in Tkinter it will generally appear slightly offset from the top left-hand corner of the screen. This is a fairly counter-intuitive location and most of the GUI driven programs that I run usually open at the centre of the page or a little higher than the center.
If you want a primer of window positioning, check out the following tutorial:
How Do I Change the Size and Position of the Main Window in Tkinter and Python 3
In Python 3, to put the main window in the center of the screen I use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Python 3, Tkinter 8.6 # Centering Root Window on Screen from tkinter import * root = Tk() # Gets the requested values of the height and widht. windowWidth = root.winfo_reqwidth() windowHeight = root.winfo_reqheight() print("Width",windowWidth,"Height",windowHeight) # Gets both half the screen width/height and window width/height positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2) positionDown = int(root.winfo_screenheight()/2 - windowHeight/2) # Positions the window in the center of the page. root.geometry("+{}+{}".format(positionRight, positionDown)) root.mainloop() |
Step By Step
When we set the geometry
of the position in Line 18, we need to know the dimensions of the user’s screen and the dimensions of the window.
Tkinter’s geometry
positions the window based on the window’s top left-hand corner. To find out this position we need to halve the width and height of the screen and subtract those by half the width and height of the window.
To find out the screen and window values we use Tkinter’s winfo
command that allows us to find out information about the windows we create with Tkinter and how they are positioned in the screen.
winfo_reqwidth and winfo_reqheight
8 9 10 11 |
# Gets the requested values of the height and widht. windowWidth = root.winfo_reqwidth() windowHeight = root.winfo_reqheight() print("Width",windowWidth,"Height",windowHeight) |
On lines 9 and 10 we create the variables for the window’s width and height.
We use win_reqheight
rather than win_height
because we have not set the size in the geometry yet. reqheight
and reqwidht
are the requested heights and width of the window. This takes into account all the sizing that might take place inside the window when we size things like labels, buttons and text boxes.
In the current example, we haven’t put anything in the window so Tkinter will use its standard height and width of 200×200. I’ve kept the print call in there for you to check for yourself.
winfo_screenwidth and winfo_screenheight
13 14 15 |
# Gets both half the screen width/height and window width/height positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2) positionDown = int(root.winfo_screenheight()/2 - windowHeight/2) |
On lines 13-15 , we create the position variables. Here we first get the screen width and height. We use the winfo_screenwidth
and winfo_screenheight
for this.
We then continue to carry out the formula to find the centre point of the screen offset by half the width and height of the window. This will often calculate as a float
that needs to be converted to an integer with int
before it can be added to the geometry
.
Adding the position to the geometry.
17 18 |
# Positions the window in the center of the page. root.geometry("+{}+{}".format(positionRight, positionDown)) |
Finally, we set the position of the window using the geometry method.
A little bonus:
I often find that most programs usually open a little above centre on the screen. To do this I divide the screen height by 3:
15 |
positionDown = int(root.winfo_screenheight()/3 - windowHeight/2) |
Want to learn how to automate your daily admin at work with Python? Udemy has some great Python automation courses that will help you learn how to automate your tasks so you can focus on what really matters.
Got a more specific problem you need help with, but don’t have the time to develop the skills? Fiverr’s your best bet to find a skilled professional to solve your problem quickly and cheaply. *
*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.
Why not use root.winfo_screenwidth() and root.winfo_screenheight() ?