[PyQt] PyQt4.Gui.QFileDialog
Hans-Peter Jansen
hpj at urpla.net
Tue Nov 1 12:30:06 GMT 2011
On Monday 31 October 2011, 08:07:00 Janwillem wrote:
> I want a small script with just a few filedialogs not a full blown
> gui application . However when I try the code below the dialog does
> not disappear after clicking the Open button on the dialog but stays
> on top until the script exits. What am I doing wrong??
> Thanks, Janwillem
>
> 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,QWidget
> if default_dir == None:
> default_dir = environ['HOME']
> app = QApplication(argv)
> fdialog = QFileDialog
> filenames = fdialog.getOpenFileNames(directory=default_dir)
> app.exit()
> return list(filenames)
>
> if __name__ == '__main__':
> from PyQt4.QtCore import QT_VERSION_STR
> from platform import python_version
> print('Python %s' % (python_version()))
> print('PyQt4 %s' %QT_VERSION_STR)
> print('open dialog')
> lst = open_files()
> print('dialog finished')
> for fname in lst:
> print(fname)
> import time
> time.sleep(2)
> print 'finished'
Without taking a deeper look, you might want to try instancing/exiting
QApplication just once per process.
Also time.sleep is a (non pyqtonic) blocking function. Use a
QTimer.singleShot for this purpose, which would allow you to let the
event loop run in the meantime.
Pete
More information about the PyQt
mailing list