PyQt5 - 如何将 Qfiledialog 带到前面? [英] PyQt5 - how to bring the Qfiledialog to the front?
问题描述
我的代码使用 PyQt 打开文件夹选择对话框.选择文件夹后,它将最小化.我希望对话框在任何其他窗口之前弹出.我还没有找到解决办法.有什么建议吗?
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")
推荐答案
我认为您的问题来自以下函数中的无".folderpath = QFileDialog.getExistingDirectory(None, "Select folder")
I think your problem comes from the "None" in the following function.
folderpath = QFileDialog.getExistingDirectory(None, "Select folder")
无法设置对话框模式,因为它没有父级.通常,我们有 self
而不是 None
.
The dialog modality cannot be set because it has no parent. Usually, instead of None
we have self
.
当然应用程序不是从 QWidget 继承的.很抱歉.
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_())
这篇关于PyQt5 - 如何将 Qfiledialog 带到前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!