Python Forum

Full Version: tkinter change the text of the checkbox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I would like to know whether it is possible to change the text of the checkbox in the middle of the program. Please see the following code:
import tkinter as tk
 

active_devices = ["device1","device2","device3","device4","device5","device6","device7","device8"]
 
def Checkbox_triggered():
    for x in range(0,len(active_devices)):
        if(checkbox_var[x].get() == 1):
            box_to_update.append(checkboxes[x].cget("text"))         
            print("devices set to remove = ",checkboxes[x].cget("text"))
        print("")
            
            
def Remove_item_gui():
    if(len(box_to_update) > 0 ):
        for x in box_to_update:
            device_serial = x.split('.')[1]
            active_devices.remove(device_serial)
            print("updated active_devices",active_devices)
            

global canvas#described as global because used outside class
master = tk.Tk()
canvas = tk.Canvas(master,bg='dim gray',width=1920,height=1080) 
checkboxes = {}
checkbox_var = {}
checkboxes_window = {}
box_to_update = []
for x in range(0,len(active_devices)):
    txt= str(x) + "."+active_devices[x]
    checkbox_var[x] = tk.IntVar()
    checkboxes[x] = tk.Checkbutton(canvas,bg="forest green",fg="black",width=12,height=2,text=txt,variable=checkbox_var[x],onvalue=1,offvalue=0,command = Checkbox_triggered)
    checkboxes_window[x] = canvas.create_window(280,(480+x*40),window=checkboxes[x])
    
remove_item_button = tk.Button(canvas, text="remove",activebackground='dim gray',bg="dim gray",bd=0,highlightthickness=0,compound=tk.CENTER,font='calibri 12 bold',command = lambda:Remove_item_gui())
canvas.create_window(960,200,window=remove_item_button)

canvas.pack()
master.mainloop()
When I click on the checkbox, I mark which device out of 8 I want to clear. When I click remove_item_button, all the ticked devices will be cleared and removed from the array. Even though they are cleared from the array, the text still remains. Whenever device is removed from the checkbox, I would like to display text "EMPTY" instead. Is it possible to change the "text=txt" value after the checkbox has been created?
I have found how to do it and its quite easy:
checkbox[x].config(text="EMPTY")