Python geometry in tkinter














































Python geometry in tkinter



Python geometry in tkinter


Tkinter is one of the modules in python which is used to develop GUI(Graphical User Interface). You donot have to install tkinter module where it comes along with python.

Tkinter provides various methods. Among them geometry( ) is used to set dimensionality to tkinter window and position of main window on desktop.

Example:

when we create a window size with no dimensionality, it creates default window size as shown.

#importing tkinter module
from tkinter import *

#creating window
window=Tk()

window.mainloop()

Output:





Using geometry method:

Example 1:

#importing tkinter module
from tkinter import *

#to create window with title 'Tk'
window=Tk()

#adding dimensionality
window.geometry('500x500')

#creating frames

frame1=Frame(window, width=250, height=150, background="black")
frame1.grid(row=0, column=0)
frame1=Frame(window, width=250, height=150, background="blue")
frame1.grid(row=0, column=1)
frame1=Frame(window, width=250, height=150, background="yellow")
frame1.grid(row=1, column=0)

window.mainloop()

Here in the example, we have created window size with '500x500' pixels using 'geometry()' by adding three frames with different colours and width,height parameters.


Output:


Example 2:

from tkinter import *
window=Tk()
window.geometry('200x200+150+250')
window.mainloop()


This creates window with size '200x200' pixels and position of window shifts 150 on Y-axis and 250 on X-axis as shown below.




Comments