唯羽
2021-04-10 10:20
采纳率: 100%
浏览 354

pyqt5 QFileDialog.getOpenFileName 选择大文件卡死问题?

select_file.py

# -*- encoding: utf-8 -*-
import os
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QFileDialog

from window.select_file import Ui_SelectFile


class SelectFile(Ui_SelectFile):
    """选择文件"""

    def __init__(self, w: QWidget, src_lis: list):
        Ui_SelectFile.__init__(self)
        self.src_lis = src_lis
        self.file_info = {'code': 0}
        self.w = w

    def setupUi(self, context):
        Ui_SelectFile.setupUi(self, context)
        self.FileSelectTipTag.setText('')
        self.SrcSelectTip.setText('')
        self.SrcSelectComboBox.addItems(self.src_lis)
        self.OpenFileButton.clicked[bool].connect(self.open_file)

    def open_file(self):
        file_name, file_type = QFileDialog.getOpenFileName(self.w,
                                                           "选取文件",
                                                           os.getcwd(),  # 起始路径
                                                           "Zip Files (*.zip)")
        print(file_name)
        print(file_type)


def show_select_file(src_lis: list):
    """
    选择文件
    :param src_lis: 路径列表
    :return:
    """
    app = QApplication(sys.argv)
    w = QWidget()
    ui = SelectFile(w, src_lis)
    ui.setupUi(w)
    w.show()
    app.exec_()
    return ui.file_info


if __name__ == '__main__':
    dir_lis = ['/', '/test_up/', '/test_up/1/']
    show_select_file(dir_lis)

生成的ui:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui\select_file.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_SelectFile(object):
    def setupUi(self, SelectFile):
        SelectFile.setObjectName("SelectFile")
        SelectFile.resize(816, 302)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/common/img/tag/9130.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        SelectFile.setWindowIcon(icon)
        SelectFile.setStyleSheet("#SelectFile\n"
                                 "{\n"
                                 "    background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(112, 213, 218, 255), stop:1 rgba(255, 255, 255, 255));\n"
                                 "}")
        self.FileSrcLineEdit = QtWidgets.QLineEdit(SelectFile)
        self.FileSrcLineEdit.setGeometry(QtCore.QRect(20, 40, 641, 41))
        self.FileSrcLineEdit.setObjectName("FileSrcLineEdit")
        self.OpenFileButton = QtWidgets.QPushButton(SelectFile)
        self.OpenFileButton.setGeometry(QtCore.QRect(680, 40, 101, 31))
        self.OpenFileButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.OpenFileButton.setStyleSheet("background-color: rgb(0, 157, 236);")
        self.OpenFileButton.setObjectName("OpenFileButton")
        self.FileSelectTag = QtWidgets.QLabel(SelectFile)
        self.FileSelectTag.setGeometry(QtCore.QRect(20, 0, 101, 41))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.FileSelectTag.setFont(font)
        self.FileSelectTag.setObjectName("FileSelectTag")
        self.FileSelectTipTag = QtWidgets.QLabel(SelectFile)
        self.FileSelectTipTag.setGeometry(QtCore.QRect(20, 90, 161, 31))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.FileSelectTipTag.setFont(font)
        self.FileSelectTipTag.setStyleSheet("color: rgb(255, 0, 0);")
        self.FileSelectTipTag.setObjectName("FileSelectTipTag")
        self.SrcSelectComboBox = QtWidgets.QComboBox(SelectFile)
        self.SrcSelectComboBox.setGeometry(QtCore.QRect(20, 160, 361, 41))
        self.SrcSelectComboBox.setObjectName("SrcSelectComboBox")
        self.SrcSelectTag = QtWidgets.QLabel(SelectFile)
        self.SrcSelectTag.setGeometry(QtCore.QRect(20, 120, 141, 41))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.SrcSelectTag.setFont(font)
        self.SrcSelectTag.setObjectName("SrcSelectTag")
        self.SrcSelectTip = QtWidgets.QLabel(SelectFile)
        self.SrcSelectTip.setGeometry(QtCore.QRect(20, 210, 161, 31))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.SrcSelectTip.setFont(font)
        self.SrcSelectTip.setStyleSheet("color: rgb(255, 0, 0);")
        self.SrcSelectTip.setObjectName("SrcSelectTip")
        self.ConfirmButton = QtWidgets.QPushButton(SelectFile)
        self.ConfirmButton.setGeometry(QtCore.QRect(30, 250, 111, 41))
        self.ConfirmButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.ConfirmButton.setStyleSheet("background-color: rgb(0, 157, 236);")
        self.ConfirmButton.setObjectName("ConfirmButton")
        self.ExitButton = QtWidgets.QPushButton(SelectFile)
        self.ExitButton.setGeometry(QtCore.QRect(190, 250, 111, 41))
        self.ExitButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.ExitButton.setStyleSheet("background-color: rgb(203, 0, 0);\n"
                                      "")
        self.ExitButton.setObjectName("ExitButton")

        self.retranslateUi(SelectFile)
        QtCore.QMetaObject.connectSlotsByName(SelectFile)

    def retranslateUi(self, SelectFile):
        _translate = QtCore.QCoreApplication.translate
        SelectFile.setWindowTitle(_translate("SelectFile", "选择文件"))
        self.OpenFileButton.setText(_translate("SelectFile", "打开"))
        self.FileSelectTag.setText(_translate("SelectFile", "请选择文件:"))
        self.FileSelectTipTag.setText(_translate("SelectFile", "文件路径不能为空"))
        self.SrcSelectTag.setText(_translate("SelectFile", "请选择上传路径:"))
        self.SrcSelectTip.setText(_translate("SelectFile", "文件路径不能为空"))
        self.ConfirmButton.setText(_translate("SelectFile", "确 定"))
        self.ExitButton.setText(_translate("SelectFile", "退 出"))


from . import upload_rc

现象:

文件小一点是正常的,文件大的时候就卡半天未响应。

其实我只需要文件的路径就可以了,怀疑是选择文件的时候它读取文件内容了。求大佬帮解,只需要路径,是不是有什么参数要设置?查也没查到

 

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 已采纳

    选取文件夹 QFileDialog.getExistingDirectory()
    选择文件 QFileDialog.getOpenFileName()
    选择多个文件 QFileDialog.getOpenFileNames()
    选择保存文件 QFileDialog.getSaveFileName()

    用 QFileDialog.getExistingDirectory()就可以了。

    打赏 评论
  • 有问必答小助手 2021-04-12 09:52

    您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    打赏 评论

相关推荐 更多相似问题