PyQt5 - how to bring the Qfiledialog to the front?

余生长醉 提交于 2021-02-10 20:15:57

问题


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")

回答1:


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_())


来源:https://stackoverflow.com/questions/49140526/pyqt5-how-to-bring-the-qfiledialog-to-the-front

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!