PyQt:1个文件选择对话框实现既可以选择文件,也可以选择文件夹
1.背景
2.解决方案
2.1.获取代码
2.2.方法1
2.3.方法2
附:取消多余的列
1.背景
用户选择输入或输出路径。一般我们使用QtWidgets.QFileDialog.getExistingDirectory选择文件夹:
使用QtWidgets.QFileDialog.getOpenFileName选择文件:
上述2种方法需要放2个按钮(1个负责输入文件,1个负责输入文件夹),虽然可以很好的解决输入或输出的问题,但是也有局限性。如最近GUI的功能变得越来越多,碍于空间的限制,需要将输入文件和文件夹合并为一个按钮,后台根据输入的是文件还是文件夹采用不同的逻辑。类似Pycharm可以同时打开文件和文件夹:
通过查询官方文档,发现确实不支持同时打开文件和文件夹(从函数名也可以看出来getOpenFileName是为文件而生,一个是为getExistingDirectory文件夹而生):
QString getExistingDirectory (QWidget parent = None, QString caption = '', QString directory = '', Options options = QFileDialog.ShowDirsOnly)
QString getOpenFileName (QWidget parent = None, QString caption = '', QString directory = '', QString filter = '', Options options = 0)
即使 getExistingDirectory不设置option=QFileDialog.ShowDirsOnly,依然看不到文件。
附:QFileDialog.Option的取值
Constant | Value | Description |
QFileDialog.ShowDirsOnly | 0x00000001 | Only show directories in the file dialog. By default both files and directories are shown. (Valid only in the Directory file mode.) |
QFileDialog.DontResolveSymlinks | 0x00000002 | Don't resolve symlinks in the file dialog. By default symlinks are resolved. |
QFileDialog.DontConfirmOverwrite | 0x00000004 | Don't ask for confirmation if an existing file is selected. By default confirmation is requested. |
QFileDialog.DontUseNativeDialog | 0x00000010 | Don't use the native file dialog. By default, the native file dialog is used unless you use a subclass of QFileDialog that contains the Q_OBJECT macro. |
QFileDialog.ReadOnly | 0x00000020 | Indicates that the model is readonly. |
QFileDialog.HideNameFilterDetails | 0x00000040 | Indicates if the file name filter details are hidden or not. |
QFileDialog.DontUseSheet | 0x00000008 | In previous versions of Qt, the static functions would create a sheet by default if the static function was given a parent. This is no longer supported and does nothing in Qt 4.5, The static functions will always be an application modal dialog. If you want to use sheets, use QFileDialog.open() instead. |
QFileDialog.DontUseCustomDirectoryIcons | 0x00000080 | Always use the default directory icon. Some platforms allow the user to set a different icon. Custom icon lookup cause a big performance impact over network or removable drives. Setting this will affect the behavior of the icon provider. This enum value was added in Qt 4.8.6. |
2.解决方案
经过在网上查询,得出如下2套解决方案。
2.1.获取代码
PyQt多重文件选择对话框(推荐,比CDSN便宜):
链接:https://pan.baidu.com/s/1y3B-bYwXx-aTfugVhoawyw?pwd=ot6e
提取码:ot6e,下载后打开“微信支付.png”使用微信扫码支付:
获取压缩包密码的2种方式:
第1种方式:付款后微信留言购买的资源名称(上方橙色文字),博主会微信回复你解压密码(推荐,不需要加好友):
→
→
第2种方式:截图支付凭证以及资源名称(上方橙色文字)在CSDN私信博主,博主会CSDN私聊你解压密码:
→
或在CSDN下载(不推荐!CSDN会随着下载次数增多而涨价,导致资源太贵)
2.2.方法1
点击图片,查看高清大图):
双击需要选择的文件或文件夹,成功获得了我们需要的路径,并成功打印出来:
self.close()取消注释即可:
2.3.方法2
点击图片,查看高清大图):
双击需要选择的文件或文件夹,成功获得了我们需要的路径,也成功打印了出来:
self.close()取消注释即可:
附:取消多余的列
注:提供的程序已默认优化,需要看到Size、Type、修改时间可将对应代码注释即可):
这是我们可以通过隐藏上述3列来实现界面的优化(也可以自动设置列宽):
self.setColumnHidden(1, True)
self.setColumnHidden(2, True)
self.setColumnHidden(3, True)
效果如图: