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
Expanding & contracting widgets along with the main window
expand & contract of widgets on change in geometry of the parent Tkinter window using rowconfigure
Zoom In and Zoom out widgets along with the window can be achieved by using the rowconfigure() and columnconfigure(). Here we are assigning weight to each row and columns to match the expansion and contraction of the main or parent window.
While placing the widgets in the grid we have to use the sticky option to expand the widget in all four directions sticky='nsew' to occupy the available space.
import tkinter as tk
my_w = tk.Tk() # parent window
my_w.geometry('200x150') # Size of the window, width & height
my_w.title("www.plus2net.com") # Adding a title
b1=tk.Button(my_w,text='Button 1',bg='lightpink',font=14)
b1.grid(row=0,column=0,padx=10,pady=10,sticky='nsew')
b2=tk.Button(my_w,text='Button 2',bg='yellow',font=14)
b2.grid(row=1,column=0,padx=10,pady=10,sticky='nsew')
my_w.rowconfigure(0,weight=1) # first row with weight
my_w.rowconfigure(1,weight=1) # second row with weight
my_w.columnconfigure(0,weight=1) # first column with weight
my_w.mainloop() # Keep the window open