gpt4 book ai didi

python - tkFileDialog 的 askopenfilename 方法中的默认文件类型

转载 作者:太空狗 更新时间:2023-10-30 01:16:43 29 4
gpt4 key购买 nike

出于某种原因,默认文件类型会根据我是使用菜单还是使用 Ctrl+O 热键打开文件对话框而改变。为什么?

from Tkinter import *
import tkFileDialog

FILEOPENOPTIONS = dict(defaultextension='.bin',
filetypes=[('Bin file','*.bin'), ('All files','*.*')])

class TestGUI(Tk):
def __init__(self):
Tk.__init__(self)
self.title('Test')
menu = self.menubar = Menu(self)
fmenu = self.filemenu = Menu(menu, tearoff=0)
menu.add_cascade(label='File', underline=0, menu=fmenu)
fmenu.add_command(label="Open", underline=0,
accelerator='Ctrl+O',
command=self.fopendialog)
self.config(menu=menu)
self.bind_all('<Control-o>', self.fopendialog)

def fopendialog(self, event=None):
print repr(tkFileDialog.askopenfilename(parent=self,
**FILEOPENOPTIONS))

if __name__ == "__main__":
test = TestGUI()
test.mainloop()

最佳答案

我有同样的问题,但我通过将默认文件扩展名放在字典的最后来修复它。

像这样:

FILEOPENOPTIONS = dict(defaultextension='.bin',
filetypes=[('All files','*.*'), ('Bin file','*.bin')])

请参阅 this page 上的示例供引用。

关于python - tkFileDialog 的 askopenfilename 方法中的默认文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11352278/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com