PyQt:1个文件选择对话框实现既可以选择文件,也可以选择文件夹

PyQt:1个文件选择对话框实现既可以选择文件,也可以选择文件夹

1.背景

2.解决方案

2.1.获取代码

2.2.方法1

2.3.方法2

附:取消多余的列


  ⚡插播一条老家自产的糖心苹果,多个品种,欢迎选购!有问题随时私信我⚡:

🍎🍎来自雪域高原的馈赠——海拔2000米的大凉山高原生态糖心苹果🍎🍎https://blog.csdn.net/qq_15969343/article/details/126107252

1.背景

        在使用PyQt写GUI时,有一个操作是比不可少的:用户选择输入或输出路径。一般我们使用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的取值

ConstantValueDescription
QFileDialog.ShowDirsOnly0x00000001Only show directories in the file dialog. By default both files and directories are shown. (Valid only in the Directory file mode.)
QFileDialog.DontResolveSymlinks0x00000002Don't resolve symlinks in the file dialog. By default symlinks are resolved.
QFileDialog.DontConfirmOverwrite0x00000004Don't ask for confirmation if an existing file is selected. By default confirmation is requested.
QFileDialog.DontUseNativeDialog0x00000010Don'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.ReadOnly0x00000020Indicates that the model is readonly.
QFileDialog.HideNameFilterDetails0x00000040Indicates if the file name filter details are hidden or not.
QFileDialog.DontUseSheet0x00000008In 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.DontUseCustomDirectoryIcons0x00000080Always 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.获取代码

  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()取消注释即可:

附:取消多余的列

 其实我们只需要看到Name这一列就可以了,Size、Type、修改时间对我们来说不需要,并且容易掩盖Name,非常不美观(注:提供的程序已默认优化,需要看到Size、Type、修改时间可将对应代码注释即可):

 这是我们可以通过隐藏上述3列来实现界面的优化(也可以自动设置列宽):

self.setColumnHidden(1, True)
self.setColumnHidden(2, True)
self.setColumnHidden(3, True)

效果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任博啥时候能毕业?

有用的话,请博主喝杯咖啡吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值