1

My aim is to create 10 dropdown lists (I want to be able to access these values later on) using a for loop to assign each StringVar() and tk.OptionMenu(). However, I am receiving a AttributeError:

'str' object has no attribute 'set'

despite my variables having assigned to them a tkinter.StringVar object:

{'self.comp0': <tkinter.StringVar object at 0x046B9890>, 'self.comp1': <tkinter.StringVar object at 0x0C5E19B0>

code:

from tkinter import OptionMenu
import tkinter as Tkinter
import tkinter.filedialog as tkFileDialog
from tkinter import messagebox as tkMessageBox


class GasGen(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
        self.grid()

    def initialize(self):

        vars= {}

        for i in range(8):
            name = "self.comp" + str(i)
            vars[name] = Tkinter.StringVar()

        menus = {}

        for num, i in enumerate(vars):
            names = "menu" + str(num)
            menus[name] = OptionMenu(self, i, "methane", "ethane", "propane", "iso-butane", "n-butane", "iso-pentane", "n-pentane", "benzene").grid(column = 0, row = num)

        print(vars)
        print(".......................")
        print(menus)

if __name__ == "__main__":
    app = GasGen(None)
    app.title('Gas mixture generator')
    app.configure(background = "slate gray")
    app.mainloop()

out:

{'self.comp0': <tkinter.StringVar object at 0x03EA3670>, 'self.comp1': <tkinter.StringVar object at 0x0BB919B0>, 'self.comp2': <tkinter.StringVar object at 0x05045690>, 'self.comp3': <tkinter.StringVar object at 0x0BBE59B0>, 'self.comp4': <tkinter.StringVar object at 0x0BBE5CB0>, 'self.comp5': <tkinter.StringVar object at 0x0BBF54B0>, 'self.comp6': <tkinter.StringVar object at 0x0BBF54F0>, 'self.comp7': <tkinter.StringVar object at 0x0BBF5530>} 

..........................

> {'menu0': None, 'menu1': None, 'menu2': None, 'menu3': None, 'menu4':
> None, 'menu5': None, 'menu6': None, 'menu7': None}

Is this not a good way to assign variables in tkinter? If not, is there a better way to dynamically generate these variables to allow to me set values to their names in the tkinter application?

1 Answer 1

3

Your syntax is wrong. The i in OptionMenu(self, i, "methane", "eth... is actually a StringVar(), whose value is set when an Optionmenu is selected. So you need to keep a list of it and initialise the corresponding one.

Here is the working code.

from tkinter import OptionMenu
import tkinter as Tkinter

class GasGen(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.vars = []
        self.initialize()
        self.grid()

    def initialize(self):
        for i in range(8):
            t = Tkinter.StringVar()
            t.set("Not Selected")
            self.vars.append(t)

        for i in range(len(self.vars)):
            OptionMenu(self, self.vars[i], "methane", "ethane", "propane", "iso-butane", "n-butane", "iso-pentane", "n-pentane", "benzene").grid(column = 0, row = i)

        Tkinter.Button(text="Show Values", command=self.show).grid(pady=10)

    def show(self):
        Tkinter.Label(text="\n".join([k.get() for k in self.vars])).grid(pady=10)

if __name__ == "__main__":
    app = GasGen(None)
    app.title('Gas mixture generator')
    app.configure(background = "slate gray")
    app.mainloop()

enter image description here

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

3 Comments

This is missing self.vars =[] right? .. it worked with this anyway, just for code completeness. Thanks.
@Joey I believe it is there.Just above the self.initialize() function.
it is indeed. Apologies.

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.