rowconfigure() & columnconfigure()


Tkinter Grid

The width for a given column ( or row ) is the width of the widest cell. These row and column sizes depends on the widget we place inside.

We can manage these sizes by using using relative weightage using rowconfigure() and columnconfigure().
root.columnconfigure(N,Weight)
root.rowconfigure(N,Weight)
N: row or column number
Weight : Relative scale for distributing extra space.

Tkinter rowconfigure & columnconfigure to assign relative weight to rows and columns width & height



Here is an example without using rowconfigure() and columnconfigure() grid without rowconfigure and columnconfigure in Tkinter

Here we used Frame as containers to hold group of widgets.
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("410x200")  # Size of the window

#my_w.columnconfigure(0,weight=1)
#my_w.rowconfigure(0, weight=1) 
#my_w.rowconfigure(1, weight=1) # change weight to 4
#my_w.rowconfigure(2, weight=1)

frame_top=tk.Frame(my_w,bg='red')
frame_middle=tk.Frame(my_w,bg='yellow')
frame_bottom=tk.Frame(my_w,bg='blue')
#placing in grid
frame_top.grid(row=0,column=0,sticky='WENS')
frame_middle.grid(row=1,column=0,sticky='WENS')
frame_bottom.grid(row=2,column=0,sticky='WENS')
#adding labels to frame 
l1=tk.Label(frame_top,text='frame_top')
l1.grid(row=0,column=0,padx=10,pady=2)

l2=tk.Label(frame_middle,text='frame_middle')
l2.grid(row=0,column=0,padx=10,pady=2)

l3=tk.Label(frame_bottom,text='frame_bottom')
l3.grid(row=0,column=0,padx=10,pady=2)
my_w.mainloop()  # Keep the window open
In above code there are some lines commented ( removed ) , by introducing these lines we can assign weightage to each row and full weightage given to single column ( column =0 )
my_w.columnconfigure(0,weight=1) # column weight 100% 
my_w.rowconfigure(0, weight=1) 
my_w.rowconfigure(1, weight=1) # change weight to 4
my_w.rowconfigure(2, weight=1)
grid with rowconfigure and columnconfigure in Tkinter
Here all three rows are equally distributed as their weight=1, so each one takes 33% of total space available. Now let us change the weight.
my_w.rowconfigure(0, weight=1) # 10% of total height
my_w.rowconfigure(1, weight=8) # 80% of total height
my_w.rowconfigure(2, weight=1) # 10% of total height
grid with 10%,80%,10% rowconfigure weight in Tkinter

columnconfigure()

We can assign weight to different columns by using columnconfigure(). Here let us add one left column ( frame_left )
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("410x200")  # Size of the window

my_w.columnconfigure(0,weight=1)
my_w.columnconfigure(1,weight=1)

my_w.rowconfigure(0, weight=1) 
my_w.rowconfigure(1, weight=8) # change weight to 4
my_w.rowconfigure(2, weight=1)

frame_top=tk.Frame(my_w,bg='red')
frame_middle=tk.Frame(my_w,bg='yellow')
frame_bottom=tk.Frame(my_w,bg='blue')
frame_left=tk.Frame(my_w,bg='lightgreen')
#placing in grid
frame_top.grid(row=0,column=1,sticky='WENS')
frame_middle.grid(row=1,column=1,sticky='WENS')
frame_bottom.grid(row=2,column=1,sticky='WENS')
frame_left.grid(row=0,column=0, rowspan=3,sticky='WENS')

my_w.mainloop()  # Keep the window open
grid with columnconfigure 50%,50% weight  in Tkinter
Let us add one right side column ( frame_right ) with different weight.
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("410x200")  # Size of the window

my_w.columnconfigure(0,weight=1)
my_w.columnconfigure(1,weight=3)
my_w.columnconfigure(2,weight=1)

my_w.rowconfigure(0, weight=1) 
my_w.rowconfigure(1, weight=8) # change weight to 4
my_w.rowconfigure(2, weight=1)

frame_top=tk.Frame(my_w,bg='red')
frame_middle=tk.Frame(my_w,bg='yellow')
frame_bottom=tk.Frame(my_w,bg='blue')
frame_left=tk.Frame(my_w,bg='lightgreen')
frame_right=tk.Frame(my_w,bg='lightblue')
#placing in grid
frame_top.grid(row=0,column=1,sticky='WENS')
frame_middle.grid(row=1,column=1,sticky='WENS')
frame_bottom.grid(row=2,column=1,sticky='WENS')
frame_left.grid(row=0,column=0,rowspan=3,sticky='WENS')
frame_right.grid(row=0,column=2,rowspan=3,sticky='WENS') 

my_w.mainloop()  # Keep the window open
grid with columnconfigure 10%,80%,10% weight in Tkinter

using rowconfigure columnfigure inside frame

We can use rowconfigure() columnfigure() outside the main window. Here we will use inside our frame. We kept three Labels inside top frame.
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("410x200")  # Size of the window

my_w.columnconfigure(0,weight=1)
my_w.columnconfigure(1,weight=3)
my_w.columnconfigure(2,weight=1)

my_w.rowconfigure(0, weight=1) 
my_w.rowconfigure(1, weight=8) # change weight to 4
my_w.rowconfigure(2, weight=1)

frame_top=tk.Frame(my_w,bg='red')
frame_middle=tk.Frame(my_w,bg='yellow')
frame_bottom=tk.Frame(my_w,bg='blue')
frame_left=tk.Frame(my_w,bg='lightgreen')
frame_right=tk.Frame(my_w,bg='lightblue')
#placing in grid
frame_top.grid(row=0,column=1,sticky='WENS')
frame_middle.grid(row=1,column=1,sticky='WENS')
frame_bottom.grid(row=2,column=1,sticky='WENS')
frame_left.grid(row=0,column=0,rowspan=3,sticky='WENS')
frame_right.grid(row=0,column=2,rowspan=3,sticky='WENS')

frame_top.columnconfigure(0,weight=1)
frame_top.columnconfigure(1,weight=3)
frame_top.columnconfigure(2,weight=1)

l1=tk.Label(frame_top,text='left')
l1.grid(row=0,column=0,padx=5,pady=5)

l2=tk.Label(frame_top,text='center')
l2.grid(row=0,column=1,padx=5)

l3=tk.Label(frame_top,text='Right')
l3.grid(row=0,column=2,padx=5)

my_w.mainloop()  # Keep the window open
grid with columnconfigure inside a frame in Tkinter
Grid Layout Pack Layout place layout Frame

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





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