Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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 不知道如何解决错误:AttributeError:';str';对象没有属性';设置';_Python_Sql_Tkinter_Pyodbc - Fatal编程技术网

Python 不知道如何解决错误:AttributeError:';str';对象没有属性';设置';

Python 不知道如何解决错误:AttributeError:';str';对象没有属性';设置';,python,sql,tkinter,pyodbc,Python,Sql,Tkinter,Pyodbc,我试图在点击选项菜单时从中获取结果。然而,我不断地得到错误 >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\samu

我试图在点击选项菜单时从中获取结果。然而,我不断地得到错误

>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'
我不知道如何解决这个问题,因为我希望能够找到用户选择的值,以便在另一个函数中使用它

如果有帮助的话,数据库中存储的单词是城市名称

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

我希望输出一个城市名称。例如
'Oslo'

如果您查看
选项菜单
小部件的文档,您会发现在
主参数
之后,您必须提供一个
变量
。此参数必须设置为特殊tkinter变量之一(
StringVar
等)

您在这个参数位置没有给它一个合适的变量,所以tkinter使用您给它的字符串作为变量。更改值时,tkinter将调用
set
方法。但是,由于传递的是字符串而不是变量,因此会出现错误,
str
没有名为
set
的方法

解决方案是给
选项菜单
一个变量。我猜您已经定义了这个变量,因为您选择了
destination\u
方法调用
destination\u choice.get()

假设
destination\u choice
是一个特殊的tkinter变量,您需要如下定义
选项菜单

OptionMenu(window, destination_choice, *temp_dest,command = destination_selected).pack()

您有名为“无”的目的地吗?为什么要将该行与“无”进行比较,而不是与
None
进行比较?在您提供的代码中,我看不到对
.set
的任何引用。请编辑您的问题以包含完整的回溯(从
回溯开始(最近一次调用最后一次):
到错误消息结束的位)。错误告诉您确切的错误:您正在对字符串调用
set
方法,而字符串没有
set
方法。为什么要用字符串调用
set
?此外,您发布的任何代码都不会导致此错误。请提供正确的。这是在调用返回self.func(*args)文件的第1705行,文件“C:\Users\samue\AppData\Local\Programs\Thonny\lib\Tkinter\u init\uupy”中运行Tkinter回调回溯(最近一次调用)中的代码异常时给出的完整错误“C:\Users\samue\AppData\Local\Programs\Thonny\lib\tkinter_init_uu.py”,第3440行,在调用self.\uu var.set(self.\uu value)AttributeError:'str'对象没有属性'set'-@BoarGules@BryanOakley如果我没有将cursor.fetchone()指定为str(),代码将无法工作,因为小部件显示为空白,没有OptionMenu。