我已经导入了 tkinter.messagebox,当我按 F5 并在 IDLE 上测试时,我的程序运行顺利。 但是,当应显示消息框时,我的程序无法在控制台上运行,并且出现错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\lib\tkinter\__init__.py", line 1456, in __call__
return self.func(*args)
File "U:\my documents\6th form\A2\Computing\F454\Code and Prototyping\trackeve
nts.py", line 204, in validation
messagebox.showerror("Data invalid", "The data you have entered is invalid.
Please make sure you have submitted it correctly.")
NameError: global name 'messagebox' is not defined
我导入消息框和代码部分(如果需要):
import tkinter.messagebox
def validation():
(...)
elif completedValidation == False:
messagebox.showerror("Data invalid", "The data you have entered is invalid. Please make sure you have submitted it correctly.")
如果您
import tkinter.messagebox
,则必须这样称呼它:用tkinter.messagebox
。如果您想将其别名为 messagebox
,则需要 from tkinter import messagebox
。
试试这个
import Tkinter
import tkMessageBox
#rest of the code and replace the below line
tkMessageBox.showerror("Data invalid", "The data you have entered is invalid. Please make sure you have submitted it correctly.")
如果 tkinter 模块是最新的,您可以尝试此操作。
from tkinter import messagebox
messagebox.showinfo("abc", "def")