Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I resize the root window in Tkinter?
In this example, we will see how to resize a tkinter window using the geometry manager. Tkinter geometry manager is generally used to configure the width and height of a tkinter window.
The geometry(width x height) method takes width and height as parameters and resizes the window accordingly. We can also define the position of the tkinter window by adding geometry("width x height+X+Y") where X and Y are horizontal and vertical positions of the window.
Basic Window Resizing
Here's how to create and resize a tkinter window ?
# Import tkinter library
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win = Tk()
# Set the geometry of tkinter frame
win.geometry("750x250+400+300")
# Create a Label widget
label1 = Label(win, text="Hello There!", font=('Courier', 20, 'underline'))
label1.pack()
win.mainloop()
The output displays a resized tkinter window with the specified dimensions and position.
Understanding Geometry String Format
The geometry string follows the format: "widthxheight+x_offset+y_offset"
from tkinter import *
win = Tk()
# Different geometry configurations
win.geometry("800x600") # Only size, no position
# win.geometry("800x600+100+50") # Size with position
# win.geometry("400x300+0+0") # Position at top-left
win.title("Resized Window Example")
label = Label(win, text="Window Size: 800x600", bg="lightblue", font=('Arial', 14))
label.pack(pady=50)
win.mainloop()
Dynamic Resizing Methods
You can resize windows dynamically using buttons or events ?
from tkinter import *
def resize_small():
win.geometry("400x300")
def resize_large():
win.geometry("800x600")
win = Tk()
win.geometry("600x400")
win.title("Dynamic Resize Example")
# Create buttons for resizing
btn_small = Button(win, text="Small Size", command=resize_small, bg="red", fg="white")
btn_small.pack(pady=10)
btn_large = Button(win, text="Large Size", command=resize_large, bg="green", fg="white")
btn_large.pack(pady=10)
status_label = Label(win, text="Click buttons to resize window", font=('Arial', 12))
status_label.pack(pady=20)
win.mainloop()
Setting Minimum and Maximum Size
You can control resize limits using minsize() and maxsize() methods ?
from tkinter import *
win = Tk()
win.geometry("600x400")
win.title("Size Constraints Example")
# Set minimum and maximum window size
win.minsize(300, 200) # Minimum width=300, height=200
win.maxsize(1000, 700) # Maximum width=1000, height=700
info_text = """
This window has size constraints:
? Minimum: 300x200
? Maximum: 1000x700
Try resizing manually!
"""
label = Label(win, text=info_text, justify=LEFT, font=('Arial', 12))
label.pack(pady=50)
win.mainloop()
Common Geometry Methods
| Method | Description | Example |
|---|---|---|
geometry() |
Set size and position | "800x600+100+50" |
minsize() |
Set minimum size | minsize(300, 200) |
maxsize() |
Set maximum size | maxsize(1000, 700) |
resizable() |
Enable/disable resizing | resizable(False, True) |
Conclusion
Use geometry() to set window size and position in Tkinter. Combine with minsize() and maxsize() to control resize limits. The format is "widthxheight+x+y" for complete window configuration.
