Tkinter Create OptionMenus With Loop

倾然丶 夕夏残阳落幕 提交于 2021-02-05 09:29:16

问题


I have created a code to create tkinter OptionMenus with values in a dictionary using a for loop. The code seems to work successfully, with OptionMenus appearing with keywords on a window as desired...

import tkinter as tk
from tkinter import *

class Example:

    def __init__(self): 
        #Dictionary with categories and their relative keywords
        self.categorykeywords={"Category 1":["Keyword 1", "Keyword 2", "Keyword 3"], "Category 2":["Keyword A","Keyword B","Keyword C"], "Category 3":["Another Keyword"]}

        #Dictionary containing the option menus referenced by category name
        btn_dict={}

        #Storing tkvar variable for later referencing
        self.dropdownreference={}

        #Number to assign to the tkvar name, to make the unique variables for each category
        i=1
        columncounter=0

        for category in self.categorykeywords:
            #Creating a unique variable / name for later reference
            exec('self.tkvar_' + str(i) + ' = ' + 'StringVar(root)') 

            #Creating OptionMenu with unique variable
            btn_dict[category] = tk.OptionMenu(root, exec('variable=self.tkvar_'+str(i)), *self.categorykeywords[category])

            btn_dict[category].grid(row=0, column=columncounter, padx=1, pady=1)

            #Storing the variable used for later use
            self.dropdownreference[category]=exec('variable=self.tkvar_'+str(i)) 

            columncounter+=1
            i+=1

root = Tk()
my_gui = Example()
root.mainloop()

However, when they are selected, I receive an error:

Traceback (most recent call last):
  File "c:\users\czuczor\appdata\local\programs\python\python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "c:\users\czuczor\appdata\local\programs\python\python36\lib\tkinter\__init__.py", line 3434, in __call__
    self.__var.set(self.__value)
AttributeError: 'NoneType' object has no attribute 'set'

I'm guessing it's having trouble actually assigning the variable, or possibly even just displaying the selected keyword. I get the same error when attempting to use ttk.OptionMenu, which automatically displays the first value. Any ideas on how to fix this?


回答1:


Thanks to the comments, here is the issue solved by using a dictionary to define the variable instead of the exec command.

import tkinter as tk

class Example:

    def __init__(self): 
        #Dictionary with categories and their relative keywords
        self.categorykeywords={"Category 1":["Keyword 1", "Keyword 2", "Keyword 3"], "Category 2":["Keyword A","Keyword B","Keyword C"], "Category 3":["Another Keyword"]}

        #Dictionary containing the option menus referenced by category name
        btn_dict={}

        #Storing tkvar variable for later referencing
        self.dropdownreference={}

        #Number to assign to the tkvar name, to make the unique variables for each category
        i=1
        columncounter=0

        for category in self.categorykeywords:
            #Creating a unique variable / name for later reference
            self.dropdownreference[category] = StringVar(root)

            #Creating OptionMenu with unique variable
            btn_dict[category] = tk.OptionMenu(root, self.dropdownreference[category], *self.categorykeywords[category])

            btn_dict[category].grid(row=0, column=columncounter, padx=1, pady=1)

            columncounter+=1
            i+=1

root = Tk()
my_gui = Example()
root.mainloop()


来源:https://stackoverflow.com/questions/49453053/tkinter-create-optionmenus-with-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!