Reputation: 1
I am trying to link each individual checkbutton to a specific entrybox using function. but i have no idea how to name each individual checkbuttons. coding is new to me and i need help desperately...
tried using a function found online but only the last entrybox is affected the checkbuttons
from tkinter import *
#initialise intial window, root
root = Tk()
dishes = Text(root, cursor="arrow")
qty = Text(root, cursor="arrow")
dishes.pack(side=RIGHT)
qty.pack(side=LEFT)
#to display a entrybox each row
for i in range(10):
ent = Entry(root, width=3, state='disabled')
qty.window_create("100.0", window=ent)
qty.insert("end", "\n")
##to display a checkbutton each row
for key in range(10):
var = IntVar(value=0)
cb = Checkbutton(dishes, text="%d" % key,
variable=var, onvalue=1, offvalue=0)
dishes.window_create("end", window=cb)
dishes.insert("end", "\n")
root.mainloop()
Upvotes: 0
Views: 40
Reputation: 385970
You can save the entries and variables in an array, and reference them using an index, assuming there's a 1:1 relationship between the entry and the checkbutton.
For the entries:
entries = []
for i in range(10):
ent = Entry(root, width=3, state='disabled')
entries.append(ent)
...
For the checkbutton variables:
vars = []
for key in range(10):
var = IntVar(value=0)
vars.append(var)
cb = Checkbutton(..., command=lambda i=key: set_state(i))
...
For the command to change the state:
def set_state(i):
new_state = "disabled" if vars[i].get() == 0 else "normal"
entries[i].configure(state=new_state)
Upvotes: 1