Tkinter geometry(): Window Size, Position and Resizing

Tkinter geometry() controls the size and screen position of a top-level window. A geometry string can contain the window width, height and its horizontal and vertical position.

import tkinter as tk

root=tk.Tk()
root.geometry('500x300+100+80')
root.mainloop()

This creates a window 500 pixels wide and 300 pixels high, positioned 100 pixels from the left and 80 pixels from the top of the screen.


Tkinter Window Geometry, Resizable and Window State

Tkinter geometry() Syntax 🔝

The complete geometry format is:

'WIDTHxHEIGHT+X+Y'

For example:

root.geometry('600x400+150+100')
PartMeaning
600Window width
400Window height
150Horizontal position from the left edge
100Vertical position from the top edge

The size or position part can be omitted when only one needs to change.

Set Window Width and Height 🔝

To set only the size:

root.geometry('500x300')

The width and height are normally specified in pixels.

Build the Geometry String from Variables

width=500
height=300

root.geometry(f'{width}x{height}')

Using an f-string is simpler than joining several converted strings.

Set the Window Position 🔝

The position can be included with the size:

root.geometry('500x300+200+120')

This requests a position 200 pixels from the left and 120 pixels from the top.

Change Only the Position

root.geometry('+200+120')

The existing requested size is retained while the window position changes.

Position Relative to the Right or Bottom Edge

A negative position value is interpreted relative to the opposite screen edge by the Tk window manager.

root.geometry('400x250-20-40')

This requests the window near the bottom-right side of the screen.

Center a Tkinter Window on the Screen 🔝

Use the screen width and height to calculate the top-left position required to center a window.

import tkinter as tk

root=tk.Tk()

width=500
height=300

screen_width=root.winfo_screenwidth()
screen_height=root.winfo_screenheight()

x=(screen_width-width)//2
y=(screen_height-height)//2

root.geometry(f'{width}x{height}+{x}+{y}')
root.mainloop()

This is useful for dialogs and small applications where an initial centered position improves usability.

Let Tkinter Use the Natural Requested Size 🔝

A fixed geometry is not always necessary. If geometry() is not called, Tkinter and its geometry managers calculate a suitable window size from the widgets inside it.

import tkinter as tk

root=tk.Tk()

tk.Label(root, text='Tkinter chooses the required window size.', padx=20, pady=20).pack()

root.mainloop()

Control Window Resizing with resizable() 🔝

Tkinter resizable window with resizing disabled

By default, a top-level Tkinter window can normally be resized horizontally and vertically by the user.

Disable All User Resizing

root.resizable(False, False)

Allow Only Horizontal Resizing

root.resizable(True, False)

Allow Only Vertical Resizing

root.resizable(False, True)

Check the Current Setting

print(root.resizable())

The returned values indicate whether width and height are currently user-resizable.

Preventing resizing can make an interface harder to use on different displays. Disable resizing only when the design genuinely requires a fixed window.

Restrict Window Size with minsize() and maxsize() 🔝

minsize() sets the smallest permitted window size.

root.minsize(300, 200)

maxsize() sets the largest permitted size.

root.maxsize(900, 700)

Complete Example

import tkinter as tk

root=tk.Tk()
root.geometry('500x350')
root.minsize(300, 200)
root.maxsize(900, 700)

root.mainloop()

This still allows resizing, but only within the specified limits.

Change Window Size Dynamically 🔝

An application can call geometry() again at any time to change its requested size.

import tkinter as tk

root=tk.Tk()
root.geometry('300x200')

width=300
height=200

def resize(change):
    global width, height

    width=max(150, width+change)
    height=max(100, height+change)
    root.geometry(f'{width}x{height}')

tk.Button(root, text='Increase Window', command=lambda: resize(20)).grid(row=0, column=0, padx=10, pady=20)
tk.Button(root, text='Decrease Window', command=lambda: resize(-20)).grid(row=0, column=1, padx=10, pady=20)

root.mainloop()

The max() calls prevent the example from requesting an impractically small window.

Maximize a Tkinter Window 🔝

On platforms that support the zoomed window state, it can be used to maximize the window:

root.state('zoomed')

This is a maximized normal window. It is not the same as fullscreen mode.

True Fullscreen Mode 🔝

Fullscreen hides normal desktop/window-manager space and requests that the Tkinter window occupy the full screen.

root.attributes('-fullscreen', True)

Exit Fullscreen with Escape

import tkinter as tk

root=tk.Tk()
root.attributes('-fullscreen', True)

def exit_fullscreen(event=None):
    root.attributes('-fullscreen', False)

root.bind('<Escape>', exit_fullscreen)
root.mainloop()

Fullscreen support and details are controlled by the platform window manager, so applications should always provide an obvious way to exit it.

Minimize, Restore and Hide a Window 🔝

Minimize

root.iconify()

Restore or Show

root.deiconify()

Hide Completely

root.withdraw()

Using state()

The common portable states include:

'normal'
'iconic'
'withdrawn'

Query the current state with:

print(root.state())

Get the Current Window Geometry 🔝

Calling geometry() without an argument returns the current size and position.

print(root.geometry())
Sample Output
500x300+200+120

The same information can also be retrieved with:

print(root.winfo_geometry())

Tkinter winfo Geometry Methods 🔝

MethodMeaning
winfo_width()Current allocated widget width.
winfo_height()Current allocated widget height.
winfo_reqwidth()Width requested by the widget based on its contents and options.
winfo_reqheight()Height requested by the widget.
winfo_screenwidth()Screen width available to Tk, in pixels.
winfo_screenheight()Screen height available to Tk, in pixels.
winfo_x()Current x position of the window/widget.
winfo_y()Current y position of the window/widget.
winfo_geometry()Current geometry string.

Example

print('Width:', root.winfo_width())
print('Height:', root.winfo_height())
print('Requested width:', root.winfo_reqwidth())
print('Screen width:', root.winfo_screenwidth())

Why update_idletasks() Is Sometimes Needed 🔝

Immediately after creating widgets, Tkinter may not yet have completed geometry calculation. Calling update_idletasks() processes pending idle work such as geometry calculations and redraw requests.

import tkinter as tk

root=tk.Tk()

label=tk.Label(root, text='A label whose requested size must be calculated.', padx=20, pady=20)
label.pack()

root.update_idletasks()

print('Window width:', root.winfo_width())
print('Requested width:', root.winfo_reqwidth())

root.mainloop()

update_idletasks() vs update()

MethodProcesses
update_idletasks()Pending idle callbacks, layout calculations and redraw work.
update()Processes a broader set of pending Tk events and can create nested/re-entrant event handling if used carelessly.

For ordinary applications, allow mainloop() to process events rather than repeatedly calling update().

Window Geometry with Dynamically Created Widgets 🔝

When the number or size of widgets changes dynamically, it is often better to let the geometry manager calculate the required window size instead of estimating the width manually.

import tkinter as tk

root=tk.Tk()
root.title('Dynamic Buttons')

languages=('PHP', 'Python', 'Perl', 'jQuery', 'Java', 'MySQL', 'CSS', 'Oracle')

for column, language in enumerate(languages):
    tk.Button(root, text=language).grid(row=0, column=column, padx=3, pady=15)

root.update_idletasks()

print('Required width:', root.winfo_reqwidth())
print('Required height:', root.winfo_reqheight())

root.mainloop()

Here Tkinter calculates the natural window size from the Buttons. This is generally more robust than assuming each Button requires exactly 50 pixels.

Tkinter Window Geometry with Dynamically Created Buttons

geometry() vs grid(), pack() and place() 🔝

geometry() controls a top-level window's requested size and position. Geometry managers such as grid() and place() arrange widgets inside that window.

FeaturePurpose
root.geometry()Top-level window size and screen position.
widget.grid()Arrange child widgets in rows and columns.
widget.pack()Arrange child widgets against sides or available space.
widget.place()Position child widgets using coordinates or relative positions.

For responsive grid layouts, see rowconfigure() and columnconfigure().

Common Tkinter Geometry Mistakes 🔝

1. Calling state('zoomed') Fullscreen

A maximized normal window and fullscreen mode are different. Use state('zoomed') only for supported maximize behaviour and attributes('-fullscreen', True) for fullscreen.

2. Hardcoding Every Window Size

Tkinter can calculate a natural requested size from its child widgets. Hardcoded dimensions can clip content when fonts, platforms or display settings change.

3. Reading Width Too Early

print(root.winfo_width())

can return an initial placeholder size before layout calculation has completed. Use update_idletasks() first when an immediate measurement is required.

4. Confusing Requested Size with Actual Size

winfo_reqwidth() is the size requested by the widget. winfo_width() is its current allocated width.

5. Using update_idletasks() to Fix Blocking Code

It only processes idle work. Long-running work should not block Tkinter's event loop.

6. Updating Tkinter Widgets Directly from Worker Threads

Keep GUI operations on the Tkinter thread. Background work can communicate results back to the GUI, which can then update widgets through the main event loop.

7. Disabling Resizing Without a UX Reason

A fixed-size window may become difficult to use on another display. Prefer sensible minimum sizes and responsive widget layouts when possible.

8. Calculating Dynamic Window Width from Widget Count Alone

Different text, fonts, themes and padding produce different widget widths. Let Tkinter calculate requested geometry where possible.

Summary of Tkinter Window Geometry 🔝

  • Use geometry('WIDTHxHEIGHT') to set window size.
  • Use geometry('WIDTHxHEIGHT+X+Y') to set size and screen position.
  • Use geometry('+X+Y') when only the position should change.
  • geometry() without an argument returns the current geometry string.
  • Use winfo_screenwidth() and winfo_screenheight() when calculating a centered position.
  • A fixed geometry is optional; Tkinter can calculate a natural size from child widgets.
  • Use resizable() to control whether users can resize width or height.
  • Use minsize() and maxsize() to restrict resizing without completely disabling it.
  • state('zoomed') is a maximize operation on supported platforms, not true fullscreen.
  • Use attributes('-fullscreen', True) for fullscreen and provide a way to exit.
  • Use iconify() to minimize, deiconify() to restore and withdraw() to hide a window.
  • winfo_width() and winfo_height() report current allocated dimensions.
  • winfo_reqwidth() and winfo_reqheight() report requested dimensions.
  • update_idletasks() can force pending geometry calculations before taking immediate measurements.
  • update_idletasks() does not solve blocking-code or threading problems.
  • Use responsive widget layouts instead of manually estimating window dimensions where possible.
  • geometry() controls the top-level window; grid(), pack() and place() arrange child widgets inside it.
Responsive Grid with rowconfigure() Sizegrip for Window Resizing Zoom Text Tkinter Clipboard

Grid Layout Place Layout Frame




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer