Python Forum

Full Version: Use String contents for checkbox name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Creating a number of checkbuttons with a for loop.
I store the name increment and would like to use the stored string as the checkbox name.
chkbox = "cb" + '%s' % i
self.chkbox  = Checkbutton(self.frame_content, text = aprlist[i-1])
         #   self.chkbox.grid(row = rowpos, column = col, padx = 5, pady = 2, sticky = 'w')
the question is, how to replace self.chkbox with the string content of chkbox

Any suggesstions?

Thanks
which graphics package are you using (I am assuming tkinter for below ... always supply all needed information about system, version of python, graphics, OS, etc.)

# chkbox = "cb" + '%s' % i
# replace with check_text = tkinter.StringVal()
    self.checkval = tkinter.IntVar()

#self.chkbox  = Checkbutton(self.frame_content, text = aprlist[i-1])
# replace with:
    self.chkbox  = Checkbutton(self.frame_content, variable=checkval, text = aprlist[i-1], command=self.show_val)
You are also going to have to create a function that gets the value of checkval:

def show_val(self, event):
    print(f"checkval is {self.checkval.get()}")