[PyQt] stand alone QFileDialog

Detlev Offenbach detlev at die-offenbachs.de
Mon May 30 09:23:03 BST 2011


On Sonntag, 29. Mai 2011, Janwillem van Dijk wrote:
> Thanks for paying attention to my question. It was about as far as I
> could get. The example below I made (unfortunaly more based on intuition
> than understanding) works when called from the command line. However,
> when called from inside the Eric5 IDE the dialog returns the results but
> hangs. This made me think my solution is incorrect.

I just tested your script in eric5 and it works ok. I could not observe your 
issue.

> 
> Any comments very welcome, cheers!
> 
> def open_files(default_dir=None):
>      """Returns list of filenames optionally given a default dir"""
>      from sys import argv
>      from os import environ
>      from PyQt4.QtGui import QApplication, QFileDialog
>      if default_dir == None:
>          default_dir = environ['HOME']
>      app = QApplication(argv)
>      filenames = QFileDialog.getOpenFileNames(directory=default_dir)
>      app.exit()
>      return list(filenames)
> 
> if __name__ == '__main__':
>      print('open dialog')
>      lst = open_files()
>      print('dialog finished')
>      for fname in lst:
>          print(fname)
> 
> for completeness the TkInter version that does work both from the
> commandline and from Eric5:
> def open_files(default_dir=None):
>      """Returns list of filenames+paths given default dir"""
>      from os import environ
>      from Tkinter import Tk
>      import tkFileDialog
>      if default_dir == None:
>          default_dir = environ['HOME']
>      root = Tk()
>      root.withdraw()  # Hide root window
>      filenames = tkFileDialog.askopenfilenames(initialdir=default_dir,
> multiple=True)
>      return list(filenames)
> 
> if __name__ == '__main__':
>      print('open dialog')
>      lst = open_files()
>      print('dialog finished')
>      for fname in lst:
>          print(fname)
> 
> On 05/27/2011 10:53 AM, Wilbert Berendsen wrote:
> > Op woensdag 11 mei 2011 schreef Janwillem:
> >> Is there for opening a file in a non-qui script a PqQt4 equivalent for
> >> the TkInter based function below?
> > 
> > look for QFileDialog.
> > http://doc.trolltech.com/4.7/qfiledialog.html and
> > http://www.riverbankcomputing.com/static/Docs/PyQt4/html/python_v3.html#q
> > filedialog
> > 
> > w best regards,
> > Wilbert Berendsen
> 
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt


-- 
Detlev Offenbach
detlev at die-offenbachs.de


More information about the PyQt mailing list