【问题标题】:"tkinter TclError: bad file type" using askopenfilename“tkinter TclError:错误的文件类型”使用 askopenfilename
【发布时间】:2016-04-09 17:13:47
【问题描述】:

这是我第一次使用 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 ...?}?

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

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

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

    或者

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

    【讨论】:

    • 非常感谢凯文!我不骗你,我在你发帖前一分钟就意识到了这一点。我想我应该在哭求帮助之前多玩一点。 :)
    猜你喜欢
    • 2018-10-05
    • 1970-01-01
    • 2012-06-25
    • 2020-11-04
    • 2020-11-11
    • 2017-11-08
    • 1970-01-01
    • 2013-01-16
    相关资源
    最近更新 更多