For some reason, the default file type changes depending on whether I open the file dialog with the menu, or withe the Ctrl+O hotkey. Why?
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()
I had this same problem, but I fixed it by putting the default file extension last in the dictionary.
Like this:
FILEOPENOPTIONS = dict(defaultextension='.bin',
filetypes=[('All files','*.*'), ('Bin file','*.bin')])
See the example on this page for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With