问题
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