Python:导入不导入messagebox

时间:2018-03-10 11:47:30

标签: python

from tkinter import *
from tkinter import messagebox

root = Tk()
messagebox.showinfo("Hello world", "you are the best")
root.mainloop()

当我使用*

导入时,为什么必须显式导入messagebox

1 个答案:

答案 0 :(得分:2)

messageboxtkinter包的子模块。

通配符导入语法不导入子模块,只导入tkinter包本身中定义的名称。

因此,您需要明确导入messagebox子模块。

参考

https://docs.python.org/3/library/tkinter.html#tkinter-modules
https://docs.python.org/3/reference/simple_stmts.html#the-import-statement

相关的tkinter来源

https://github.com/python/cpython/blob/3.6/Lib/tkinter/__init__.py
https://github.com/python/cpython/blob/3.6/Lib/tkinter/messagebox.py