【问题标题】:I'm trying to make an exit button for the page and for some reason it has error and i don't know how to solve it我正在尝试为该页面制作一个退出按钮,但由于某种原因它有错误,我不知道如何解决
【发布时间】: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


【解决方案1】:

askyesnotkinter.messagebox 模块中定义。从那里导入它:

from tkinter.messagebox import askyesno

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多