召唤狮 发表于 2021-2-5 14:38:07

打开文件报错

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

Daniel_Zhang 发表于 2021-2-5 14:46:12

from tkinter import filedialog

Twilight6 发表于 2021-2-5 14:49:37


filedialog 等一些组件在 * 导入是不被导入的,要重新进行导入

参考代码:

from tkinter import *
from tkinter import filedialog

root = Tk()
def callback():
   fileName = filedialog.askopenfilename()
   print(fileName)
Button(root, text="打开文件", command=callback).pack()
mainloop() #点打开文件按钮报错

逃兵 发表于 2021-2-5 14:49:55

from tkinter import *
from tkinter import filedialog

root = Tk()
def callback():
   fileName = filedialog.askopenfilename()
   print(fileName)
Button(root, text="打开文件", command=callback).pack()
mainloop() #点打开文件按钮报错
页: [1]
查看完整版本: 打开文件报错