Python Forum
Use String contents for checkbox name - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Use String contents for checkbox name (/thread-22998.html)



Use String contents for checkbox name - jcday - Dec-06-2019

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


RE: Use String contents for checkbox name - Larz60+ - Dec-06-2019

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()}")