简体   繁体   中英

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

I would like that QFileDialog in PyQt5 opens in default browser dowload location. I have this code, which opens last used location, because of '' empty third parameter. How can I read this information in Windows and 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)

Here's a possible solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM