如何在 Python 中使用 QFileDialog 打开默认浏览器下载位置?

How to open default browser download location with QFileDialog in Python?

我希望 PyQt5 中的 QFileDialog 在默认浏览器下载位置打开。我有这段代码,它打开了最后使用的位置,因为 '' 第三个参数为空。如何在 Windows 和 Linux 中阅读此信息?

 def selectfile_Dialog(self, event=None):

        fname, _ = QtWidgets.QFileDialog.getOpenFileName(
            self, 'Open File', '', 'Binary executable (*.exe)', None)
        # sender is object that sends the signal
        sender = self.sender()
        # write the selected file name into that QLineEdit widget 'list1_lineEdit'
        sender.setText(fname)

这是一个可能的解决方案:

import sys
import webbrowser
import os
import winreg

from PyQt5.Qt import *  # noqa


def get_download_path():
    if os.name == 'nt':
        sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
        downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
            location = winreg.QueryValueEx(key, downloads_guid)[0]
        return location
    else:
        return os.path.join(os.path.expanduser('~'), 'downloads')


def main():
    app = QApplication(sys.argv)
    fname, _ = QFileDialog.getOpenFileName(
        None, 'Open File', get_download_path(), 'Binary executable (*.exe)', None
    )
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()