Opening a file dialog

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nitriles
    New Member
    • Oct 2007
    • 1

    Opening a file dialog

    Hello,
    i have a program in which i plot 10 subplots with matplotlib. To launch the program i now type in the console:
    python dataplot.py DATAFILE_10.10. 07.csv
    However what i want is that python asks me for a file which i can then select in a sort of browse window. How to implement this?
    I though of creating a GUI with Qt in which you browse (which i dont know how to do) and then click on the button PLOT which should launch the program dataplot with the datafile that has ben selected.
    Hope someone can help me.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by nitriles
    Hello,
    i have a program in which i plot 10 subplots with matplotlib. To launch the program i now type in the console:
    python dataplot.py DATAFILE_10.10. 07.csv
    However what i want is that python asks me for a file which i can then select in a sort of browse window. How to implement this?
    I though of creating a GUI with Qt in which you browse (which i dont know how to do) and then click on the button PLOT which should launch the program dataplot with the datafile that has ben selected.
    Hope someone can help me.
    It's pretty simple using Tkinter. There's even a way to hide the Tk main window (I think):[CODE=python]from Tkinter import Tk
    from tkFileDialog import askopenfilename


    if __name__ == '__main__':
    root=Tk()
    fileName = askopenfilename ()
    root.destroy()
    print fileName
    [/CODE]

    Comment

    • dshimer
      Recognized Expert New Member
      • Dec 2006
      • 136

      #3
      When working in windows, I use the code that I have pasted below. At one time I looked at all the interesting things you could do with this simple example, but have just been copying, pasting, and making minor mods for years. I have to say though that within win32 it is about as simple and straight forward as I could find.
      Code:
      >>> import win32ui,win32con
      >>> fd = win32ui.CreateFileDialog(1,'','', 0,'Text |*.txt')
      >>> fd.SetOFNTitle("Select Text File")
      >>> if fd.DoModal() == win32con.IDCANCEL: sys.exit(1)
      ... 
      >>> print fd.GetPathName().replace('\\','/')
      C:/users/dennis/python/forum.txt
      >>>
      If I want to select multiple files and have the names returned as a list I would do something like.
      Code:
      import win32ui,win32con,sys
      fd = win32ui.CreateFileDialog(1,None,None,win32con.OFN_ALLOWMULTISELECT,'All Files|*.*')
      fd.SetOFNInitialDir('c:\\tmp')
      fd.SetOFNTitle('Select multiple files')
      if fd.DoModal() == win32con.IDCANCEL:sys.exit(1)
      print fd.GetPathNames()
      Which if I run it and select some random files gives me something like...
      Code:
      ['C:\\tmp\\Stadium.png', 'C:\\tmp\\BucksX4.png', 'C:\\tmp\\ShiftProblem.png', 'C:\\tmp\\BucksX1.png']

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by dshimer
        When working in windows, I use the code that I have pasted below. At one time I looked at all the interesting things you could do with this simple example, but have just been copying, pasting, and making minor mods for years. I have to say though that within win32 it is about as simple and straight forward as I could find.
        Code:
        >>> import win32ui,win32con
        >>> fd = win32ui.CreateFileDialog(1,'','', 0,'Text |*.txt')
        >>> fd.SetOFNTitle("Select Text File")
        >>> if fd.DoModal() == win32con.IDCANCEL: sys.exit(1)
        ... 
        >>> print fd.GetPathName().replace('\\','/')
        C:/users/dennis/python/forum.txt
        >>>
        If I want to select multiple files and have the names returned as a list I would do something like.
        Code:
        import win32ui,win32con,sys
        fd = win32ui.CreateFileDialog(1,None,None,win32con.OFN_ALLOWMULTISELECT,'All Files|*.*')
        fd.SetOFNInitialDir('c:\\tmp')
        fd.SetOFNTitle('Select multiple files')
        if fd.DoModal() == win32con.IDCANCEL:sys.exit(1)
        print fd.GetPathNames()
        Which if I run it and select some random files gives me something like...
        Code:
        ['C:\\tmp\\Stadium.png', 'C:\\tmp\\BucksX4.png', 'C:\\tmp\\ShiftProblem.png', 'C:\\tmp\\BucksX1.png']
        A great solution, D. It's nice to know that you are still around.

        Comment

        Working...