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
Python - geometry method in Tkinter
Python's Tkinter library provides the geometry() method to control the size and position of GUI windows. This method is essential for creating properly sized and positioned applications.
Syntax
window.geometry("widthxheight+x_offset+y_offset")
Where:
- width and height define window dimensions in pixels
- x_offset and y_offset set position on screen (optional)
Basic Window Sizing
Here's how to create a window with specific dimensions ?
from tkinter import *
base = Tk()
base.geometry('200x200')
stud = Button(base, text='Tutorialspoint', font=('Courier', 14, 'bold'))
stud.pack(side=TOP, pady=6)
mainloop()
This creates a 200x200 pixel window with a button positioned at the top.
Window with Web Link
You can create interactive elements within the sized window ?
import webbrowser
from tkinter import *
def open_url():
url = webbrowser.open_new("http://tutorialspoint.com")
main = Tk()
main.geometry("300x250")
stud = Button(main, text="Visit Tutorialspoint",
font=('Courier', 15, 'bold'),
command=open_url)
stud.pack(side=RIGHT, pady=6)
main.mainloop()
This creates a 300x250 pixel window with a clickable button that opens a web browser.
Window Positioning
Control both size and screen position using offset values ?
from tkinter import *
window = Tk()
window.geometry("400x300+100+50") # 400x300 size, positioned at (100,50)
label = Label(window, text="Positioned Window", font=('Arial', 16))
label.pack(pady=20)
window.mainloop()
This positions the window 100 pixels from the left edge and 50 pixels from the top of the screen.
Common Parameters
| Format | Description | Example |
|---|---|---|
| "widthxheight" | Sets window size only | "500x400" |
| "+x+y" | Sets position only | "+200+100" |
| "widthxheight+x+y" | Sets both size and position | "300x200+150+75" |
Conclusion
The geometry() method is essential for controlling Tkinter window dimensions and positioning. Use "widthxheight" for sizing and "+x+y" offsets for precise screen placement.
