[Python] QFileDialog not closing after file selection

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple function that opens a file dialog using pyqt6 to select a file. The function works, but the dialog hangs after being called and I'm not sure why it's not getting automatically closed when the file is selected.

    def ui_find_file(title=None, initialdir=None, file_type=None):
    import os
    from PyQt6.QtWidgets import QApplication, QFileDialog

    app = QApplication([])
    file_dialog = QFileDialog()
    file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile)

    if file_type:
    filter = f"{file_type} files (*.{file_type})"
    file_dialog.setNameFilter(filter)
    else:
    file_dialog.setNameFilter("All files (*);;")

    if initialdir is None:
    initialdir = os.getcwd()
    elif initialdir == '~':
    initialdir = os.path.expanduser('~')

    file_dialog.setDirectory(initialdir)

    if title:
    file_dialog.setWindowTitle(title)

    if file_dialog.exec():
    file_path = file_dialog.selectedFiles()[0]
    print(f"Selected {file_path}")
    file_dialog.deleteLater() # Clean up the file dialog window
    app.quit() # Exit the event loop
    return file_path
    else:
    print("No file selected")
    file_dialog.deleteLater() # Clean up the file dialog window
    app.quit() # Exit the event loop
    return None



    When I try the first lines in the command line

    from PyQt6.QtWidgets import QApplication, QFileDialog

    app = QApplication([])
    file_dialog = QFileDialog()
    file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile)


    and I use either file_dialog.open() or file_dialog.exec(), I can select a file and the dialog closes without freezing the python instance. I don't even have to call file_dialog.deleteLater() for it to close itself.

    When I try to use this from another script, for example doing:

    from utils import ui_find_file
    ui_find_file()


    It produces the freezing behavior.

    Updates on things I have tried but didn't work


    I am running this in python 3.10, Ubuntu 20.04, from a terminal.

    • I have tried using destroy() instead of destroyLater().
    • I have tried using app.processEvents() below destroy() with del(app)

    The reason I am creating app = QApplication([]) was because when I didn't, I got QWidget: Must construct a QApplication before a QWidget (which I can reproduce by commenting out the line)

    Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from utils import ui_find_file
    >>> ui_find_file()
    QWidget: Must construct a QApplication before a QWidget
    Aborted (core dumped)

    Continue reading...

Compartilhe esta Página