" tkinter TclError:错误的文件类型"使用askopenfilename

时间:2015-07-30 19:12:42

标签: python python-3.x tkinter

这是我第一次使用Tkinter。我已经导入了它,直到这一刻它一直在努力。文件类型似乎有问题吗?我也在Mac上,如果这有任何区别的话。

这是我的代码:

def importTracks(self):
    self.fname = askopenfilename(filetypes=(("Mp3 Files", "*.mp3")))

这是我收到的错误,

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/accudeveloper/PycharmProjects/AccuAdmin2.0/AccuAdmin2.0.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1533, in __call__
    return self.func(*args)
  File "/Users/accudeveloper/PycharmProjects/AccuAdmin2.0/AccuAdmin2.0.py", line 68, in importTracks
    self.fname = askopenfilename(filetypes=(("Mp3 Files", "*.mp3")))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/filedialog.py", line 375, in askopenfilename
    return Open(**options).show()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/commondialog.py", line 48, in show
    s = w.tk.call(self.command, *w._options(self.options))
_tkinter.TclError: bad file type "*.mp3", should be "typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?

1 个答案:

答案 0 :(得分:16)

filetypes=(("Mp3 Files", "*.mp3"))相当于filetypes=("Mp3 Files", "*.mp3")。我猜你打算将外括号对作为一个元组,但这需要一个尾随逗号。或者你可以使用一个列表。

self.fname = askopenfilename(filetypes=(("Mp3 Files", "*.mp3"),))

或者

self.fname = askopenfilename(filetypes=[("Mp3 Files", "*.mp3")])