[英]How to open default browser download location with QFileDialog in Python?
I would like that QFileDialog
in PyQt5
opens in default browser dowload location. 我希望
PyQt5
中的QFileDialog
在默认浏览器下载位置打开。 I have this code, which opens last used location, because of ''
empty third parameter. 我有这段代码,由于
''
空的第三个参数,它打开了上一个使用的位置。 How can I read this information in Windows and Linux? 如何在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)
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.