Python | geometry method in Tkinter

Tkinter is a Python module which is used to develop GUI (Graphical User Interface). It comes along with the Python so one do not has to install but can be done using pip command.

Tkinter provides various universal method one of them is geometry() method. This method is used to set the dimensions of the Tkinter window as well as it 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 size of the window is also small as shown in 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 on different position (300 shifted on Y-axis and 400 shifted on X-axis).

Note: We can also pass variable argument in the geometry method but it should be in the form (variable1) x (variable2) otherwise it will raise an error.



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.




Article Tags :

Be the First to upvote.


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