[PyQt] stand alone QFileDialog

Janwillem van Dijk jwevandijk at xs4all.nl
Sun May 29 21:07:20 BST 2011


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.

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#qfiledialog
>
> w best regards,
> Wilbert Berendsen
>



More information about the PyQt mailing list