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()
It defines the width and height of the initial Tkinter window where we generally place our widgets. To set the position of the Tkinter window while omitting the width and height, we can define the specification in the geometry manager. Run the code to display a window that is set to its content width and height.
In Tkinter, the position and size of a window on the screen is determined by geometry. The following shows the geometry specification: The width is the window’s width in pixels. The height is the window’s height in pixels.
To get the current title of a window, you use the title () method with no argument: In Tkinter, the position and size of a window on the screen is determined by geometry. The following shows the geometry specification: The width is the window’s width in pixels. The height is the window’s height in pixels. The x is the window’s horizontal position.
In Tkinter, the position and size of a window on the screen is determined by geometry. The following shows the geometry specification: The width is the window’s width in pixels. The height is the window’s height in pixels. The x is the window’s horizontal position.
You can omit the width and height from the geometry specification. For example:
root.geometry('+%d+%d'%(x,y))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With