简体   繁体   中英

PyQt5 - how to bring the Qfiledialog to the front?

My code uses PyQt to open up a folder select dialog. Once a folder is selected it is minimized. I'd like for the dialog to pop up in front of any other windows. I haven't been able to find a solution yet. Any suggestions?

from sys import executable, argv
from subprocess import check_output
from PyQt5.QtWidgets import QFileDialog, QApplication

def gui_fname(directory=''):
    file = check_output([executable, __file__, directory])
    return file.strip()

if __name__ == "__main__":
    directory = argv[1]
    app = QApplication([directory])
    folderpath = QFileDialog.getExistingDirectory(None, "Select folder")

I think your problem comes from the "None" in the following function. folderpath = QFileDialog.getExistingDirectory(None, "Select folder")

The dialog modality cannot be set because it has no parent. Usually, instead of None we have self .

EDIT: Of cource app is not inheriting from QWidget. Sorry about that.

use this instead. I tested it an it work:

import sys
from subprocess import check_output
from PyQt5.QtWidgets import QFileDialog, QApplication, QWidget

def gui_fname(directory=''):
    file = check_output([executable, __file__, directory])
    return file.strip()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    wid = QWidget()
    folderpath = QFileDialog.getExistingDirectory(wid, "Select folder")
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM