https://stackoverflow.com/questions/24738104/python-tkinter-8-5-import-messagebox
The following code runs fine within IDLE, but otherwise I get \"NameError: global name \'messagebox\' is not defined\". However, if I explicitly state from tkinter import messagebox, it runs fine from where ever.
from tkinter import *
from tkinter import ttk
root = Tk()
main = ttk. (root)
messagebox.showinfo(\"My \", \"My message\", icon=\"warning\", parent=main )
Why does IDLE not need the explicit import statement but elsewhere it is required?、
the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter:
from tkinter import *
doesn\'t import messagebox
it has to be explicitly imported like so:
from tkinter import messagebox
in the same way that ttk has to be imported explicitly
the reason it works in idle is because idle imports messagebox for its own purposes, and because of the way idle works, its imports are accessible while working in idle
IDLE is written in Python and uses Tkinter for the GUI, so it looks like your program is using the import statements that IDLE itself is using. However, you should explicitly include the importstatement for the messagebox if you want to execute your program outside the IDLE process.
继续阅读与本文标签相同的文章
-
华为鸿蒙成第五大操作系统,有希望超过iOS吗?
2026-05-19栏目: 教程
-
先破产再回国 贾跃亭这条路行不通
2026-05-19栏目: 教程
-
圆通速递决战双十一:率先实行涨价策略,其实取胜的关键不在于此
2026-05-19栏目: 教程
-
为什么要拥有一个区块链节点?
2026-05-19栏目: 教程
-
揭秘军运村里的吃、住、行和黑科技!
2026-05-19栏目: 教程
