Hilea Hilea - 3 years ago 206
Python Question

how to set the position of a tkinter window without setting the dimensions

i know a way to put a tkinter window lets say in the middle

from tkinter import *
root=Tk()
w=350
h=285
ws=root.winfo_screenwidth()
hs=root.winfo_screenheight()
x=(ws/2)-(w/2)
y=(hs/2)-(h/2)
root.geometry('%dx%d+%d+%d'%(w,h,x,y))


in the above code the tkinter window will pop up in the middle with a height of 285 and a width of 350. but if i added too many widget the height 285 may not be enough. fort example lets say we added this code

for x in range(50):
Label(root,text=x).pack()


only the first 13 numbers will be in the window since i set the height to a fixed number 285.

so my question is would there be a way to put a tkinter window in the middle of the screen without setting the height and width, i mean if the height and the width are not set to a fixed number the window will continue to expand automatically.for example in the code below it expands automatically except the window is not in the middle.

from tkinter import *
win=Tk()
for x in range(50):
Label(win,text=x).pack()

Answer Source

You can omit the width and height from the geometry specification. For example:

root.geometry('+%d+%d'%(x,y))  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download