I have created a set (6) of checkbuttons
in a tkinter
app using a for-loop. So far I have just created and laid them out but the don't do anything. What I want them to do is tell another function how to work depending on which checkbutton
is clicked but when I try to access the checkbuttons
I'm getting the error that's posted at the bottom of the question.
I have tried making all the buttons as individual codelines but obviously that was a lot of repeated code so instead I made them with a for loop and stored them in a nested dict
like so:
for i in self.atts:
self.att_buttons[i] = {}
self.att_buttons[i]["checkbutton"] = tk.Checkbutton(
self.check_frame, text=i, font=("Courier", 15),
onvalue = 1, offvalue = 0,
).pack(side=tk.LEFT)
I'm not sure this is right but I'm new and I'm doin my best.
I have a roll()
function and what I want is for the checkbuttons to modify the result of that function and so what I've attempted is
def roll(self):
"""Roll dice, add modifer and print a formatted result to the UI"""
value = random.randint(1, 6)
if self.att_buttons["Str"]["checkbutton"].get() == 1:
result = self.character.attributes["Strength"]["checkbutton].get()
self.label_var.set(f"result: {value} + {result} ")
File "main_page.py", line 149, in roll
if self.att_buttons["Str"]["checkbutton"].get() == 1:
AttributeError: 'NoneType' object has no attribute 'get'
Now I assume this is because I'm calling the nested dict
incorrectly but I have tried moving my code around and trying different bits and pieces and I keep getting the same error.
Update
based on hugo's answer below I have edited the for loop to be
for i in self.atts:
self.att_buttons[i] = {}
self.att_buttons[i]["checkbutton"] = tk.Checkbutton(
self.check_frame, text=i, font=("Courier", 15),
variable = tk.BooleanVar()#this is the change
)
self.att_buttons[i]["checkbutton"].pack(side=tk.LEFT)`
How would I call variable
for specific checkbuttons in my roll() function?