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

时间:2018-05-25 08:46:21

标签: python pyqt qfiledialog

我希望QFileDialog中的PyQt5在默认浏览器下载位置中打开。我有这个代码,打开上次使用的位置,因为''空第三个参数。如何在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)

1 个答案:

答案 0 :(得分:1)

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

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