NameError:未定义全局名称“messagebox” - CONSOLE MODE

时间:2015-12-03 10:21:40

标签: python tkinter messagebox

我已经导入了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.")

2 个答案:

答案 0 :(得分:2)

如果您import tkinter.messagebox,则必须如此引用:tkinter.messagebox。如果您想将其别名为messagebox,则需要from tkinter import messagebox

答案 1 :(得分:0)

试试这个

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.")