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 )
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
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
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
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