Python | geometry method in Tkinter

Tkinter is a Python module which is used to develop GUI (Graphical User Interface) applications. It comes along with the 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing only those functions which are needed
from tkinter import Tk, mainloop, TOP
from tkinter.ttk import Button
  
# creating tkinter window
root = Tk()
  
button = Button(root, text = 'Geeks')
button.pack(side = TOP, pady = 5)
  
mainloop()

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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')
  
button = Button(root, text = 'Geeks')
button.pack(side = TOP, pady = 5)
  
mainloop()

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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')
  
button = Button(root, text = 'Geeks')
button.pack(side = TOP, pady = 5)
  
mainloop()

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : abergquist

Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.