【发布时间】:2023-01-30 00:29:32
【问题描述】:
我正在尝试使用代码制作退出按钮 我在这里导入 tkinter*
from tkinter import*
from tkinter import ttk
from PIL import Image,ImageTk
import os
def iExit(self):
self.iExit=tkinter.askyesno("Face Recognition","Are you sure you want to exit?")
if self.iExit >0:
self.root.destroy()
else:
return
我得到了错误
我知道我不能将 tkinter 放在代码中,但我不知道如何解决它。我得到的错误在这里
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "c:\Users\user\Desktop\FYP\Facial_Recognition system\Main.py", line 148, in iExit
self.iExit=tkinter.askyesno("Face Recognition","Are you sure you want to exit?")
^^^^^^^
NameError: name 'tkinter' is not defined
【问题讨论】:
-
您还没有导入名称
tkinter。但是,您已经导入了所有内容从tkinter模块,所以只需askyesno而不是tkinter.askyesno就可以了。 -
我在
tkinter起飞后得到同样的错误。NameError: name 'askyesno' is not defined -
试试
ttk.askyesno -
完成尝试
ttk.askyesno并出现另一个不同的错误AttributeError: module 'tkinter.ttk' has no attribute 'askyesno'
标签: python tkinter button exit