Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

tkinter

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()
like image 809
EHM Avatar asked Oct 01 '17 07:10

EHM


People also ask

How do I set the position of a Tkinter window?

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.

What determines the size of a window in Tkinter?

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.

How to get the current title of a window in Tkinter?

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.

What is geometry In Tkinter?

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.


1 Answers

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

root.geometry('+%d+%d'%(x,y))  
like image 56
Bryan Oakley Avatar answered Oct 05 '22 23:10

Bryan Oakley