Python tkinter import error

2019-09-21 21:11发布

问题:

from tkinter import *
from tkinter import messagebox

root= Tk()
root.mainloop()

Error:

...Traceback (most recent call last): File "C:\Users\Lee\Desktop\Python projects\tkinter.py", line 1, in import tkinter as Tk File "C:\Users\Lee\Desktop\Python projects\tkinter.py", line 2, in from tkinter import messagebox ImportError: cannot import name 'messagebox'

[Finished in 25.248s] I have no idea why messagebox won't import?

回答1:

You named your file tkinter.py, so when you import tkinter, it's importing that file rather than the real tkinter module.

Rename your file to just about anything else.