f_done=('Times',22,'overstrike') # font values to use
f_normal=('Times',22,'normal') # font values to use
def my_upd():
if(var.get()==True):
ck.config(font=f_done,fg='green')
else:
ck.config(font=f_normal,fg='blue')
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x300")
f_done=('Times',22,'overstrike') # font values to use
f_normal=('Times',22,'normal') # font values to use
def my_upd():
if(var.get()==True):
ck.config(font=f_done,fg='green')
else:
ck.config(font=f_normal,fg='blue')
var=tk.BooleanVar() # variable
ck = tk.Checkbutton(my_w, text='My task No :1',
variable=var,onvalue=True,offvalue=False,font=f_normal,fg='blue',
command=lambda: my_upd())
ck.grid(row=0,column=0,padx=10,pady=25)
my_w.mainloop()
my_dict={'a':'My Task No 1','b':'My Task No 2','c':'My Task No 3'}
The method my_dict.keys()
returns a list of keys and using this inside a for loop, we can create one checkbutton dynamically for each task. for k in my_dict.keys(): # Number of checkbuttons or tasks
var=tk.BooleanVar() # variable connected to Checkbutton
ck = tk.Checkbutton(my_w, text=my_dict[k],
variable=var,onvalue=True,offvalue=False,font=f_normal,fg='blue',
command=lambda k=k: my_upd(k))
ck.grid(row=i,column=0,padx=80,pady=5,sticky='e')
Inside the for loop, after creating the checkbutton we are storing the reference of checkbutton ck and the associated variable var in a dictionary my_ref.
my_ref={} # Storing the references
i=1 # row number ( after using 0 row number for Label at top)
for k in my_dict.keys(): # Number of checkbuttons or tasks
var=tk.BooleanVar() # variable connected to Checkbutton
ck = tk.Checkbutton(my_w, text=my_dict[k],
variable=var,onvalue=True,offvalue=False,font=f_normal,fg='blue',
command=lambda k=k: my_upd(k))
ck.grid(row=i,column=0,padx=80,pady=5,sticky='e')
my_ref[k]=[ck,var] # to hold the references
i=i+1 # increase the row number
Full code is here.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x300")
f_done=('Times',22,'overstrike')
f_normal=('Times',22,'normal')
def my_upd(k): # k is the key of the reference dictionary
if(my_ref[k][1].get()==True): # checkbox is checked
my_ref[k][0].config(font=f_done,fg='green')
else: # Checkbox is unchecked
my_ref[k][0].config(font=f_normal,fg='blue')
l1=tk.Label(my_w,text='Task List',
font=('Times',32,('bold','underline')),fg='red')
l1.grid(row=0,column=0,padx=5,pady=10)
my_dict={'a':'My Task No 1','b':'My Task No 2','c':'My Task No 3'}
my_ref={} # Storing the references
i=1 # row number ( after using 0 row number for Label at top)
for k in my_dict.keys(): # Number of checkbuttons or tasks
var=tk.BooleanVar() # variable connected to Checkbutton
ck = tk.Checkbutton(my_w, text=my_dict[k],
variable=var,onvalue=True,offvalue=False,font=f_normal,fg='blue',
command=lambda k=k: my_upd(k))
ck.grid(row=i,column=0,padx=80,pady=5,sticky='e')
my_ref[k]=[ck,var] # to hold the references
i=i+1 # increase the row number
my_w.mainloop()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.