PyQt5 - 如何将 Qfiledialog 置于最前面?
PyQt5 - how to bring the Qfiledialog to the front?
我的代码使用 PyQt 打开文件夹 select 对话框。 selected 文件夹后,它会被最小化。我希望对话框在任何其他 windows 的前面弹出。我还没有找到解决办法。有什么建议吗?
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")
我认为您的问题来自以下函数中的 "None"。
folderpath = QFileDialog.getExistingDirectory(None, "Select folder")
无法设置对话模态,因为它没有父项。通常,我们有 self
而不是 None
。
编辑:应用程序当然不是从 QWidget 继承的。抱歉。
改用这个。我测试了它,它可以工作:
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_())
我的代码使用 PyQt 打开文件夹 select 对话框。 selected 文件夹后,它会被最小化。我希望对话框在任何其他 windows 的前面弹出。我还没有找到解决办法。有什么建议吗?
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")
我认为您的问题来自以下函数中的 "None"。
folderpath = QFileDialog.getExistingDirectory(None, "Select folder")
无法设置对话模态,因为它没有父项。通常,我们有 self
而不是 None
。
编辑:应用程序当然不是从 QWidget 继承的。抱歉。
改用这个。我测试了它,它可以工作:
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_())