Python tkinter grid

Basics of Python Tkinter

Layout of the page to place the components in rows and columns. We will keep one Label along with an Entry box.

Understanding row and column

Horizontal placement is done by rows and vertical placement is done by columns.
Tkinter row and column
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x200")  

l1 = tk.Label(my_w, text='row=1,column=1 ')
l1.grid(row=1,column=1) 

l2 = tk.Label(my_w, text=' row=1,column=2')
l2.grid(row=1,column=2) 

l3 = tk.Label(my_w, text='row=2,column=1 ')
l3.grid(row=2,column=1) 

l4 = tk.Label(my_w, text=' row=2,column=2')
l4.grid(row=2,column=2) 

my_w.mainloop()
The top line is row=1 and second line is row=2, similarly first vertical column is column=1 and next vertical column is column=2

sticky option

If the component size is less than the available grid space, then we can use 'E','W','N','S' to align right left top and bottom.

Check F Name and L Name is aligned to left and right. We can align the components to top and bottom also.
Tkinter grid sticky
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x100")  
my_w.title("plus2net.com")  # Adding a title

l1 = tk.Label(my_w,  text='F Name (sticky=\'W\')' ) #added one Label 
l1.grid(row=1,column=1,sticky='W') 

e1 = tk.Entry(my_w,   width=10,bg='yellow') # added one Entry box
e1.grid(row=1,column=2) 

l2 = tk.Label(my_w,  text='L Name (sticky=\'E\')')  # 
l2.grid(row=2,column=1,sticky='E') 

e2 = tk.Entry(my_w,   width=10,bg='yellow') # added one Entry box
e2.grid(row=2,column=2)

l3 = tk.Label(my_w,  text='Add Door No and Street address')   
l3.grid(row=3,column=1,sticky='W') 

e3 = tk.Entry(my_w,   width=10,bg='yellow') # added one Entry box
e3.grid(row=3,column=2)

my_w.mainloop()

columnspan option

By default columnspan=1, we can set to different value by incresing it to expand more than one column.
Tkinter grid columnspan
l1 = tk.Label(my_w, text='First', width=20)
l1.grid(row=1,column=1) 
l2 = tk.Label(my_w, text='Second', width=20)
l2.grid(row=1,column=2) 

l3 = tk.Label(my_w, text='Both First and Second, columnspan=2')
l3.grid(row=2,column=1,columnspan=2)

rowspan options

We can use rowspan=3 to expand to three rows.
Tkinter grid rowspan
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x150")  
l1 = tk.Label(my_w, text='First', width=20)
l1.grid(row=1,column=1) 
l2 = tk.Label(my_w, text='Second', width=20)
l2.grid(row=2,column=1) 
l3 = tk.Label(my_w, text='Third', width=20)
l3.grid(row=3,column=1) 

t1 = tk.Text(my_w,  height=3, width=8,bg='yellow') #text box
t1.grid(row=1,column=2,rowspan=3)

my_w.mainloop()

ipadx,ipady,padx,pady options

ipadx and ipady adds padding to the internal padding of the widget boarder.
Tkinter grid ipadx ipady
t1 = tk.Text(my_w,  height=3, width=10,bg='yellow') # text box
t1.grid(row=1,column=2,rowspan=3,ipadx=25,ipady=25)
padx and pady adds padding to the grid boarder.
Tkinter grid padx pady
t1 = tk.Text(my_w,  height=3, width=10,bg='yellow') # text box
t1.grid(row=1,column=2,rowspan=3,padx=25,pady=25)

grid_size()

This will return a tuple showing first empty columns and rows . In this example we have used upto row=3 and column=3 so we will get output as (4,4).
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x150")  
l1 = tk.Label(my_w, text='First', width=8)
l1.grid(row=1,column=1) 
l2 = tk.Label(my_w, text='Second', width=8)
l2.grid(row=2,column=2) 
l3 = tk.Label(my_w, text='Third', width=8)
l3.grid(row=3,column=3) 

print(my_w.grid_size()) # tuple  columns , rows 

my_w.mainloop()
Output
(4, 4)
You can remove the last element and check the output. We will set the value as column=2 for last element.
l3 = tk.Label(my_w, text='Third', width=8)
l3.grid(row=3,column=2) 
Output
(3, 4)

grid_slaves(row=None, column=None)

We will get a list of widgets manage by the grid.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x150")  
l1 = tk.Label(my_w, text='First', width=8)
l1.grid(row=1,column=1) 
l2 = tk.Label(my_w, text='Second', width=8)
l2.grid(row=2,column=2) 
l3 = tk.Label(my_w, text='Third', width=8)
l3.grid(row=3,column=2) 

print(my_w.grid_slaves()) 

my_w.mainloop()
Output
[<tkinter.Label object .!label3>, <tkinter.Label object .!label2>,
 <tkinter.Label object .!label>]
We can use row and column number to get the widget.
print(my_w.grid_slaves(2,2)) 
Output
[<tkinter.Label object .!label2>]

grid_remove()

Remove the widget from the perticular grid. Can be added again.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x150")  
l1 = tk.Label(my_w, text='First', width=8)
l1.grid(row=1,column=1) 
l2 = tk.Label(my_w, text='Second', width=8)
l2.grid(row=2,column=2) 
l3 = tk.Label(my_w, text='Third', width=8)
l3.grid(row=3,column=2) 
l3.grid_remove()
my_w.mainloop()
To restore the widget in same location we have to just use grid()
l3.grid()

grid_forget()

Remove the widget from the perticular grid. Can be added again at the new location. We have to specify the location while restoring, by default it takes the next new location.
l3.grid_forget()
To restore at the same location we have to specify the row and column.
l3.grid(row=3,column=2)

Removing after some time delay

We can use the widget as message box and want to remove it after 300 seconds.
l3.after(3000, lambda: l3.grid_remove() )

Pack Layout in Tkinte Python Tkinter Text


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer