python - tk messagebox import confusion -
i'm beginning learn tkinter @ moment, , when importing messagebox found must not understand import statements.
the thing confuses me that:
import tkinter tk def text_box(): if tk.messagebox.askokcancel("quit", "never mind"): root.destroy() root = tk.tk() button = tk.button(root, text="press button", command=text_box) button.pack() root.mainloop()
compiles fine, pressing button gives error 'module' object has no attribute 'messagebox'
, while code:
import tkinter tk tkinter import messagebox ... if messagebox.askokcancel("quit", "never mind"): ...
...works without hitch.
i similar error if import from tkinter import *
.
the tkinter shows messagebox
in list of package contents
, can't load in normal way.
so question is, why...and importing don't understand?
just thought should mention—the code works in python 3, , in python 2.x messagebox
called tkmessagebox
, not defined in tkinter
.
tkinter.messagebox
module, not class.
as isn't imported in tkinter.__init__.py
, explicitly have import before can use it.
import tkinter tkinter.messagebox # raise importerror tkinter import messagebox tkinter.messagebox # it's available eiter `messagebox` or `tkinter.messagebox`
Comments
Post a Comment