Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter使用循环创建选项菜单_Python_Dictionary_For Loop_Tkinter_Optionmenu - Fatal编程技术网

Python Tkinter使用循环创建选项菜单

Python Tkinter使用循环创建选项菜单,python,dictionary,for-loop,tkinter,optionmenu,Python,Dictionary,For Loop,Tkinter,Optionmenu,我创建了一个代码,使用for循环在字典中创建tkinter OptionMenus和值。代码似乎工作成功,OptionMenus显示在窗口中,并根据需要显示关键字 import tkinter as tk from tkinter import * class Example: def __init__(self): #Dictionary with categories and their relative keywords self.catego

我创建了一个代码,使用for循环在字典中创建tkinter OptionMenus和值。代码似乎工作成功,OptionMenus显示在窗口中,并根据需要显示关键字

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()
但是,当选择它们时,我收到一个错误:

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'

我猜它在实际分配变量时遇到了问题,甚至可能只是显示选定的关键字。我在尝试使用ttk.OptionMenu时遇到了相同的错误,它会自动显示第一个值。关于如何解决这个问题有什么想法吗?

多亏了这些评论,这里的问题是通过使用字典来定义变量而不是exec命令来解决的

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()

多亏了这些注释,使用字典而不是exec命令来定义变量就解决了这个问题

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()

可能的重复摆脱所有的
exec
恐惧,学习如何使用字典,为了对神圣事物的热爱。不要两次导入tkinter。从tkinter导入中删除
*
,只需将
导入tkinter作为tk
。使用
exec
也不是很省钱。你应该避免这种方法。@Aran Fey,你是说问题只是exec命令?坦白说,我不在乎这是“恐怖”还是“邪恶”。我只是需要一些有用的东西。此外,我知道如何使用字典,这一点在示例中应该很清楚。您没有帮助我。是的,问题是exec命令。你为什么不使用字典而不是exec?可能是重复的摆脱所有那些
exec
恐惧,学习如何使用字典,因为爱一切神圣的东西。不要两次导入tkinter。从tkinter导入中删除
*
,只需将
导入tkinter作为tk
。使用
exec
也不是很省钱。你应该避免这种方法。@Aran Fey,你是说问题只是exec命令?坦白说,我不在乎这是“恐怖”还是“邪恶”。我只是需要一些有用的东西。此外,我知道如何使用字典,这一点在示例中应该很清楚。您没有帮助我。是的,问题是exec命令。你为什么不用字典而不用exec?