Skip to content
Related Articles

Related Articles

Python | geometry method in Tkinter
  • Difficulty Level : Basic
  • Last Updated : 12 Jan, 2021

Tkinter is a Python module that is used to develop GUI (Graphical User Interface) applications. It comes along with Python, so you do not have to install it using the pip command.

Tkinter provides many methods; one of them is the geometry() method. This method is used to set the dimensions of the Tkinter window and is used to set the position of the main window on the user’s desktop.

Code #1: Tkinter window without using geometry method. 
 

Python3




# importing only those functions which are needed
from tkinter import Tk, mainloop, TOP
from tkinter.ttk import Button
 
# creating tkinter window
root = Tk()
 
# Create Button and add some text
button = Button(root, text = 'Geeks')
# pady is used for giving some padding in y direction
button.pack(side = TOP, pady = 5)
 
# Execute Tkinter
root.mainloop()

Output: 
 



As soon as you run the application, you’ll see the position of the Tkinter window is at the north-west position of the screen and the size of the window is also small as shown in the output. 
  
Code #2: 
 

Python3




# importing only those functions which
# are needed
from tkinter import Tk, mainloop, TOP
from tkinter.ttk import Button
 
# creating tkinter window
root = Tk()
 
# creating fixed geometry of the
# tkinter window with dimensions 150x200
root.geometry('200x150')
 
# Create Button and add some text
button = Button(root, text = 'Geeks')
button.pack(side = TOP, pady = 5)
 
# Execute Tkinter
root.mainloop()

Output: 
 

After running the application, you’ll see that the size of the Tkinter window is changed, but the position on the screen is same. 
  
Code #3: 
 

Python3




# importing only those functions which
# are needed
from tkinter import Tk, mainloop, TOP
from tkinter.ttk import Button
 
# creating tkinter window
root = Tk()
 
# creating fixed geometry of the
# tkinter window with dimensions 150x200
root.geometry('200x150 + 400 + 300')
 
# Create Button and add some text
button = Button(root, text = 'Geeks')
button.pack(side = TOP, pady = 5)
 
# Execute Tkinter
root.mainloop()

Output: 
 

When you run the application, you’ll observe that the position and size both are changed. Now the Tkinter window is appearing at a different position (300 shifted on Y-axis and 400 shifted on X-axis).
Note: We can also pass a variable argument in the geometry method, but it should be in the form (variable1) x (variable2); otherwise, it will raise an error.
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :