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
  tkinter text widget word wrap position chrisdb 6 3,003 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 2,460 Mar-10-2021, 04:21 PM
Last Post: Sir
  How to dynamically change radiobutton text kenwatts275 2 1,358 Mar-05-2021, 02:25 AM
Last Post: deanhystad
Photo Tkinter TEXT background image _ShevaKadu 5 4,081 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  Tkinter menu font size -method to change tonycat 2 3,120 Oct-11-2020, 02:43 AM
Last Post: tonycat
  tkinter | Button color text on Click Maryan 2 1,539 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Tkinter - How can I change the default Notebook border color? TurboC 4 6,663 Oct-06-2020, 10:35 PM
Last Post: TurboC
  [Tkinter] Text Upload LearningLittlebyLittle 0 903 Sep-04-2020, 07:55 PM
Last Post: LearningLittlebyLittle
  [tkinter] color change for hovering over button teacher 4 4,540 Jul-04-2020, 06:33 AM
Last Post: teacher
  [PyQt] Increase text size and change color based on temp pav1983 5 1,279 Jun-22-2020, 10:52 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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