打开文件报错
from tkinter import *root = Tk()
def callback():
fileName = filedialog.askopenfilename()
print(fileName)
Button(root, text="打开文件", command=callback).pack()
mainloop() #点打开文件按钮报错
报错内容:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\anaconda3\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "<pyshell#5>", line 2, in callback
NameError: name 'filedialog' is not defined from tkinter import filedialog
filedialog 等一些组件在 * 导入是不被导入的,要重新进行导入
参考代码:
from tkinter import *
from tkinter import filedialog
root = Tk()
def callback():
fileName = filedialog.askopenfilename()
print(fileName)
Button(root, text="打开文件", command=callback).pack()
mainloop() #点打开文件按钮报错 from tkinter import *
from tkinter import filedialog
root = Tk()
def callback():
fileName = filedialog.askopenfilename()
print(fileName)
Button(root, text="打开文件", command=callback).pack()
mainloop() #点打开文件按钮报错
页:
[1]