簡體   English   中英

如何在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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM