DanielHill123 DanielHill123 - 1 month ago 13
Python Question

NameError: global name 'messagebox' is not defined - CONSOLE MODE

I've imported tkinter.messagebox, and my program runs smoothly when I F5 and test it on IDLE.
However my program doesn't run on console when a messagebox should be displayed, and I get the error:

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


My importing of messagebox and the section of code if it's needed:

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

Answer

If you import tkinter.messagebox, you have to refer to it as such: with tkinter.messagebox. If you want to alias it to just messagebox, you need from tkinter import messagebox.