How to set the position of a Tkinter window without setting the dimensions?

Tkinter windows are executed after initializing the object of the Tkinter frame or window. We can define the size of the Tkinter window or frame using the geometry manager. 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.

Understanding Window Positioning

The geometry() method accepts a string in the format "widthxheight+x+y". When you omit the width and height, you can use just "+x+y" to position the window without specifying its dimensions. The window will automatically size itself based on its content.

Example

Here's how to position a Tkinter window at coordinates (500, 300) without setting dimensions ?

# Import the required Libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win = Tk()

# Set the geometry of tkinter frame without specifying width and height
x = 500
y = 300
win.geometry("+%d+%d" %(x,y))

# Add a Label widget
label = Label(win, text=" Tkinter has a variety of inbuilt functions, " "modules, and packages.", font = ('Georgia', 20))
label.pack(pady=10)

win.mainloop()

Alternative Methods

Using String Concatenation

from tkinter import *

win = Tk()

# Position window at (200, 150) using string concatenation
win.geometry("+" + str(200) + "+" + str(150))

label = Label(win, text="Window positioned at (200, 150)")
label.pack(padx=20, pady=20)

win.mainloop()

Using f-string (Python 3.6+)

from tkinter import *

win = Tk()

# Position window using f-string formatting
x_pos, y_pos = 300, 100
win.geometry(f"+{x_pos}+{y_pos}")

label = Label(win, text="Modern f-string positioning")
label.pack(padx=20, pady=20)

win.mainloop()

Key Points

  • The + signs before coordinates are required syntax
  • Negative values are allowed to position windows relative to screen edges
  • The window will auto-size based on its content when dimensions are omitted
  • Position coordinates are in pixels from the top-left corner of the screen

Conclusion

Use geometry("+x+y") to position a Tkinter window without setting dimensions. The window will automatically resize to fit its content while appearing at the specified screen coordinates.

Updated on: 2026-03-25T23:33:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements