【问题标题】:Tkinter Create OptionMenus With LoopTkinter 使用循环创建 OptionMenus
【发布时间】:2018-09-02 07:56:18
【问题描述】:

我创建了一个代码来使用 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 的恐惧,学习如何使用字典,以热爱一切神圣的事物。
  • 不要两次导入 tkinter。删除from tkinter import * 并保留import tkinter as tk。使用exec 也不是很省钱。你应该避免这种方法。
  • @Aran-Fey,你是说问题只是 exec 命令?坦率地说,我不在乎它是“恐怖”还是“邪恶”。我只需要一些有用的东西。另外,我知道如何使用字典,这在示例中应该很清楚。你没有帮助我。
  • 是的,问题是执行命令。为什么不使用字典而不是 exec?

标签: python dictionary for-loop tkinter optionmenu


【解决方案1】:

感谢 cmets,这是通过使用字典而不是 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()

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多