Geometry method in Tkinter | Python

In this tutorial, we are going to learn the geometry method in Tkinter. Tkinter is a module used for GUI(Graphical User Interface) in Python. There are many libraries that provide Graphical User Interface. Tkinter is the most commonly used library in python for Graphical User Interface.

Geometry method in Tkinter

The geometry method provides features like window size, dimensions, and position of the window on the screen.

Here in this tutorial, we will see the difference between using the geometry method and without using this method.

Case 1:

In this case, we try without any geometry method. Now, add a button. Observe how the window appears.

from tkinter import *

# creating an object of tkinter
tkobj = Tk()
tkobj.title("Code speedy")

but = Button(tkobj,text="Click me")

#displaying button on the main display
but.pack()

#starting the object
tkobj.mainloop()

Output:

Geometry method in Tkinter

Here the button appears at the top-left corner which exactly fits with the button.

Case 2:

In this case, we try the geometry method.

from tkinter import *

# creating an object of tkinter
tkobj = Tk()
tkobj.title("Code speedy")

#adding geometry
tkobj.geometry("300x300")

but = Button(tkobj,text="Click me")

#displaying button on the main display
but.pack()

#starting the object
tkobj.mainloop()

Output:

Geometry method in Tkinter

Here we can see there is an extra space than the button. This is because of the geometry method.

In the input of geometry method “var1 x var2” var1, var2 defines the dimensions of the window.

Leave a Reply

Your email address will not be published.