0

I'm trying to generate multiple ComboBoxes with values from a "config.ini" file, the config.ini file data is:

priority1 = Normal:farty-blobble-fx.wav:2
priority8 = Reclamacao:buzzy-blop.wav:3
priority3 = Critico:farty-blobble-fx.wav:5
priority2 = Urgente:echo-blip-thing.wav:4

and the goal is turning the sound files names to the select values in the comboboxes.

GUI

My code to generate the comboboxes is:

content_data = []
for name, value in parser.items(section_name):
    if name=="name":
        self.note.add(self.tab2, text = value)
    else:
        data_prior = value.split(":")
        self.PRIOR_LABEL = Label(self.tab2, text=data_prior[0])
        self.PRIOR_LABEL.grid(row=data_prior[2],column=0,pady=(10, 2),padx=(40,0))

        self.PRIOR_SOUNDS = None
        self.PRIOR_SOUNDS = None
        self.box_value = StringVar()
        self.PRIOR_SOUNDS = Combobox(self.tab2, textvariable=self.box_value,state='readonly',width=35)
        self.PRIOR_SOUNDS['values'] = getSoundsName()
        self.PRIOR_SOUNDS.current(int(getSoundsName().index(data_prior[1])))
        self.PRIOR_SOUNDS.grid(row=data_prior[2],column=1,pady=(10, 2),padx=(30,0))

        self.PLAY = Button(self.tab2)
        self.PLAY["width"] = 5
        self.PLAY["text"] = "Play"
        self.PLAY["command"] =  lambda:playSound(self.PRIOR_SOUNDS.get())
        self.PLAY.grid(row=data_prior[2], column=3,pady=(10,2),padx=(5,0))

And i was unable to show the current values of the "config.ini" file in the comboboxes. Thank you in advance.

7
  • 1
    What's your question? You've told us what you want and you've shown some code, but you didn't ask anything. Is the code throwing an error? Is it giving the wrong behavior? What debugging have you done to solve this? Commented Dec 17, 2015 at 11:52
  • Don´t have any error displayed. And it's simply don't work. I guess the problem is when i generate the multiple comboboxes, but i don't know another way to generate them. Commented Dec 17, 2015 at 11:55
  • does getsoundname() return the same thing every time it's called? Commented Dec 17, 2015 at 12:36
  • Yes, it does. Return a list of audio files from a folder. Commented Dec 17, 2015 at 12:38
  • what is x in the expression str( -1 * x)? Commented Dec 17, 2015 at 12:51

1 Answer 1

2

The problem is that you're creating more than one combobox, yet you keep overwriting the variables in each iteration of the loop. At the end of the loop, self.PRIOR_SOUNDS will always point to the last combobox that you created. The same is true for self.box_value, self.PLAY, etc.

The simplest solution is to use an array or dictionary to store all of your variables. A dictionary lets you reference each widget or variable by name; using a list lets you reference them by their ordinal position.

A solution using a dictionary would look something like this:

self.combo_var = {}
self.combo = {}
for name, value in parser.items(section_name):
    ...
    self.combo_var[name] = StringVar()
    self.combo[name] = Combobox(..., textvariable = self.combo_var[name])
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

Can you provide a simple example of it?
Hello Bryan, do you have an example ? I'm looking to do something similar but can't find a way yo make it work
@f42: I don't understand what you're asking. This answer has an example.
well, I will try to use your example and look for a solution. if not I'll open a new post because it's complicated to explain here

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.