1

I have a function which creates a dictionary depending on the file the user put in parameter.

And then I create a GUI which create as many CheckButtons as there are elements in dictionary.

Here is my code :

for element in self.listdiagram.dict_diagrams :
    self.diagramVar = IntVar()
    self.diagram = Checkbutton(self.window, text=element, variable=self.diagramVar, onvalue=1, offvalue=0)
    self.diagram.pack(side = BOTTOM)
self.validate = Button(self.window, text="Validate", command=self.validateCallBack, width=15, height=3)
self.validate.pack(side = BOTTOM)

The problem is i can't access to the state of a specific checkbutton as they have all same variable name.

And I don't know how can I name them differently.

I tried to create variable name with dictionary element's name with setattr(self, element, 0) or with exec but it doesn't worked because str(element) can be very long.

How can i do this ?

Thanks for helping.

1 Answer 1

2

A very simple fix to this would be to use a list, and just add each Checkbutton to the list.

For example:

self.diagramVars = []

for element in self.listdiagram.dict_diagrams :
    diagramVar = IntVar()
    diagram = Checkbutton(self.window, text=element, variable=diagramVar, onvalue=1, offvalue=0)
    diagram.pack(side = BOTTOM)

    self.diagramVars.append(diagramVar)

self.validate = Button(self.window, text="Validate", command=self.validateCallBack, width=15, height=3)
self.validate.pack(side = BOTTOM)

Q: For the moment i just want to print the state of each checkbuttons when i push the validate button but my next step is for example if i have self.diagramVars = [1, 0, 0] so the first checkButton is checked and so i want to run another function with the name (or the text) of the checked checkButton in paramater i don't know if i'm clear ?

I believe that this for loop which creates a dictionary with the element as the key will be a suitable solution (if element is able to be used as text=element option then it should be fine to be used as a key):

self.diagramVars = {}

for element in self.listdiagram.dict_diagrams :
    diagramVar = IntVar()
    diagram = Checkbutton(self.window, text=element, variable=diagramVar, onvalue=1, offvalue=0)
    diagram.pack(side = BOTTOM)

    self.diagramVars[element] = diagramVar

self.validate = Button(self.window, text="Validate", command=self.validateCallBack, width=15, height=3)
self.validate.pack(side = BOTTOM)

When you create this dictionary, to retrieve the value of a CheckButton, you will use the line diagramVars[element].get() this will access the "real time" state of the CheckButton

Sign up to request clarification or add additional context in comments.

16 Comments

Thanks a lot ! I'm trying instead of creating a list to create a dictionnary like this : diagramVars= {self.diagramVar : element} or diagramVars= {element : self.diagramVar} but in both case i have an error.
How would you expect to pull a CheckButton element from this dictionary? Like how do you want to access a specific check button, because right now I don't have the full context of the reason you need a dictionary.
@Lucas No, that's the issue, when you do that, you are setting the self.diagramVars[element] to the int of when the CheckButton is created, basically you are doing this self.diagramVars[element] = 0, because the dragramVars initial value is 0
Using self.diagramVars[element] = diagramVar to assign the dictionary value, and self.diagramVars[element].get() to retrieve the dictionary value would make it an access of the "real time" value
Ok i get it ! Thanks a lot !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.