出于某种原因,默认文件类型会根据我是使用菜单打开文件对话框还是使用 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()