Python Forum
tkinter change the text of the checkbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter change the text of the checkbox
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):
            print("devices set to remove = ",checkboxes[x].cget("text"))
def Remove_item_gui():
    if(len(box_to_update) > 0 ):
        for x in box_to_update:
            device_serial = x.split('.')[1]
            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())

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:

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] choose checkbox devilonline 1 1,270 Feb-17-2023, 01:23 PM
Last Post: Axel_Erfurt
  [Tkinter] Updating tkinter text BliepMonster 5 5,918 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,675 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,871 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] Tkinter don't change the image DQT 2 1,602 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,814 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,728 May-23-2022, 03:44 PM
Last Post: bigmac
  [Tkinter] Dynamic checkbox treeview issue OogieM 8 4,885 Mar-20-2022, 02:10 PM
Last Post: OogieM
  Can't get tkinter button to change color based on changes in data dford 4 3,410 Feb-13-2022, 01:57 PM
Last Post: dford
  tkinter text widget word wrap position chrisdb 6 7,526 Mar-18-2021, 03:55 PM
Last Post: chrisdb

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020