我试图在单击OptionMenu时获取结果。但是我不断收到错误消息。
qs5
我不知道如何解决此问题,因为我希望能够找到用户选择的值,以便可以在另一个函数中使用它。
如果有帮助,存储在数据库中的单词就是城市名称。
>Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\samue\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\samue\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py", line 3440, in __call__
self.__var.set(self.__value)
AttributeError: 'str' object has no attribute 'set'
我希望输出城市名称。 EG def destination_selected() :
selection = destination_choice.get()
print(selection)
def destination_dropdownbox_GUI():
destination = []
cursor.execute('SELECT DISTINCT Destination FROM FlightTable ')
row = str(cursor.fetchone())
x = 0
for x in row :
row = str(cursor.fetchone())
row = re.sub(r'\W+', '', row)
destination.append(row)
if row == 'None' :
break
destination.pop()
print(destination)
temp_dest = []
temp_dest = destination
print(temp_dest)
destination_dropdownbox = OptionMenu(window, *temp_dest,command = destination_selected).pack()
答案 0 :(得分:1)
如果查看OptionMenu
小部件的文档,您会发现在master
参数后必须提供variable
。此参数必须设置为特殊的tkinter变量之一(StringVar
等)。
您在此参数位置没有给它适当的变量,因此tkinter会将您给它的字符串用作变量。更改值时,tkinter将调用set
方法。但是,由于传递的是字符串而不是变量,因此会出现以下错误:str
没有名为set
的方法。
解决方案是给OptionMenu
一个变量。我猜想自您的destination_selected
方法调用destination_choice.get()
以来,您已经定义了此变量。
假设destination_choice
是一个特殊的tkinter变量,则需要这样定义OptionMenu
:
OptionMenu(window, destination_choice, *temp_dest,command = destination_selected).pack()