Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I choose where I want my Tkinter window to be located?

I am making a weather app and the window always appears in random places on the screen, how can I choose where the window appears every time?

I tried using this: root.geometry(‘250×150+0+0’) where the first two parameters are the width and height of the window. The last two parameters are x and y screen coordinates. I tried to find the exact coordinates of the center of the screen, but it was not working.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can use this to center your Tkinter window.

w = 400 # width for the Tk root
h = 600 # height for the Tk root

# get screen width and height
ws = app.winfo_screenwidth() # width of the screen
hs = app.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
app.geometry('%dx%d+%d+%d' % (w, h, x, y))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading