Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default file type in tkFileDialog's askopenfilename method

Tags:

python

tkinter

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()
like image 300
Santiclause Avatar asked Oct 08 '22 07:10

Santiclause


1 Answers

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.

like image 110
Jordan Carroll Avatar answered Oct 10 '22 21:10

Jordan Carroll