朝朝暮暮的个人主页

http://www.qtcn.org/bbs/u/192428  [收藏] [复制]

朝朝暮暮

  • 1

    关注

  • 1

    粉丝

  • 1

    访客

  • 等级:新手上路
  • 总积分:2
  • 保密,2011-11-16

最后登录:2020-09-18

更多资料

日志

基于PyQt的文件复制功能(支持复制文件夹)

2020-02-15 18:11

.UI文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>widget</class>
<widget class="QWidget" name="widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>384</width>
    <height>112</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>文件拷贝</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <property name="spacing">
      <number>0</number>
     </property>
     <item>
      <widget class="QLineEdit" name="lineEdit_src">
       <property name="frame">
        <bool>false</bool>
       </property>
       <property name="readOnly">
        <bool>true</bool>
       </property>
       <property name="clearButtonEnabled">
        <bool>false</bool>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QToolButton" name="toolButton_src">
       <property name="enabled">
        <bool>true</bool>
       </property>
       <property name="text">
        <string>...</string>
       </property>
       <property name="toolButtonStyle">
        <enum>Qt::ToolButtonIconOnly</enum>
       </property>
       <property name="autoRaise">
        <bool>true</bool>
       </property>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_2">
     <property name="spacing">
      <number>0</number>
     </property>
     <item>
      <widget class="QLineEdit" name="lineEdit_dest">
       <property name="frame">
        <bool>false</bool>
       </property>
       <property name="readOnly">
        <bool>true</bool>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QToolButton" name="toolButton_dest">
       <property name="text">
        <string>...</string>
       </property>
       <property name="autoRaise">
        <bool>true</bool>
       </property>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_3">
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QPushButton" name="pushButton_copy">
       <property name="text">
        <string>复制</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_2">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
    </layout>
   </item>
  </layout>
</widget>
<resources/>
<connections/>
</ui>

.UI 转 Py文件
class Ui_widget(object):
    def setupUi(self, widget):
        widget.setObjectName("widget")
        widget.resize(384, 112)
        self.verticalLayout = QtWidgets.QVBoxLayout(widget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.lineEdit_src = QtWidgets.QLineEdit(widget)
        self.lineEdit_src.setFrame(False)
        self.lineEdit_src.setReadOnly(True)
        self.lineEdit_src.setClearButtonEnabled(False)
        self.lineEdit_src.setObjectName("lineEdit_src")
        self.horizontalLayout.addWidget(self.lineEdit_src)
        self.toolButton_src = QtWidgets.QToolButton(widget)
        self.toolButton_src.setEnabled(True)
        self.toolButton_src.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
        self.toolButton_src.setAutoRaise(True)
        self.toolButton_src.setObjectName("toolButton_src")
        self.horizontalLayout.addWidget(self.toolButton_src)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setSpacing(0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.lineEdit_dest = QtWidgets.QLineEdit(widget)
        self.lineEdit_dest.setFrame(False)
        self.lineEdit_dest.setReadOnly(True)
        self.lineEdit_dest.setObjectName("lineEdit_dest")
        self.horizontalLayout_2.addWidget(self.lineEdit_dest)
        self.toolButton_dest = QtWidgets.QToolButton(widget)
        self.toolButton_dest.setAutoRaise(True)
        self.toolButton_dest.setObjectName("toolButton_dest")
        self.horizontalLayout_2.addWidget(self.toolButton_dest)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem)
        self.pushButton_copy = QtWidgets.QPushButton(widget)
        self.pushButton_copy.setObjectName("pushButton_copy")
        self.horizontalLayout_3.addWidget(self.pushButton_copy)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem1)
        self.verticalLayout.addLayout(self.horizontalLayout_3)

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

    def retranslateUi(self, widget):
        _translate = QtCore.QCoreApplication.translate
        widget.setWindowTitle(_translate("widget", "文件拷贝"))
        self.toolButton_src.setText(_translate("widget", "..."))
        self.toolButton_dest.setText(_translate("widget", "..."))
        self.pushButton_copy.setText(_translate("widget", "复制"))

main.Py文件
import sys, os
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QMessageBox
from Widget import Ui_widget


class PWidget(QWidget, Ui_widget):
    __pathSrc = ''
    __pathDest = ''

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initGUI()
        self.connects()

    def closeEvent(self, event):
        pass

    def initGUI(self):
        self.pushButton_copy.setEnabled(False)

    def connects(self):
        self.toolButton_src.clicked.connect(self.slot_src)
        self.toolButton_dest.clicked.connect(self.slot_dest)
        self.lineEdit_src.textChanged[str].connect(self.slot_textChang)
        self.lineEdit_dest.textChanged[str].connect(self.slot_textChang)
        self.pushButton_copy.clicked.connect(self.slot_copyFolder)

    def isPathAllow(self):
        if len(self.__pathSrc) == 0 or len(self.__pathDest) == 0:
            return False
        if (not os.path.isdir(self.__pathSrc)) or (not os.path.isdir(self.__pathDest)):
            return False
        if self.__pathSrc == self.__pathDest:
            return False
        return True

    def copyFolder(self, src, dst):
        files = os.listdir(src)
        print(files)
        for file in files:
            path = os.path.join(src, file)
            if os.path.isfile(path):
                with open(path, 'rb') as rstream:
                    container = rstream.read()
                    path = os.path.join(dst, file)
                    with open(path, 'wb') as wstream:
                        wstream.write(container)
            else:
                nextPathSrc = os.path.join(src, file)
                nextPathDest = os.path.join(dst, file)
                os.mkdir(nextPathDest)
                self.copyFolder(nextPathSrc, nextPathDest)

    @pyqtSlot()
    def slot_src(self):
        self.__pathSrc = QFileDialog.getExistingDirectory(self, '来源', os.path.curdir)
        self.lineEdit_src.setText(self.__pathSrc)
        if len(self.__pathSrc) != 0 and len(self.__pathDest) != 0:
            self.pushButton_copy.setEnabled(True)

    @pyqtSlot()
    def slot_dest(self):
        self.__pathDest = QFileDialog.getExistingDirectory(self, '目的', os.path.curdir)
        self.lineEdit_dest.setText(self.__pathDest)
        if len(self.__pathSrc) != 0 and len(self.__pathDest) != 0:
            self.pushButton_copy.setEnabled(True)

    @pyqtSlot(str)
    def slot_textChang(self, text):
        print(text)
        if len(text) == 0:
            self.pushButton_copy.setEnabled(False)

    @pyqtSlot()
    def slot_copyFolder(self):
        isPathOK = self.isPathAllow()
        if isPathOK:
            self.copyFolder(self.__pathSrc, self.__pathDest)
            messAge = QMessageBox(QMessageBox.Information, '提示', '复制成功', QMessageBox.NoButton, self)
            messAge.addButton(self.tr('好的'), QMessageBox.YesRole)
            messAge.exec_()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = PWidget()
    widget.show()
    sys.exit(app.exec_())
分类:默认分类|回复:2|浏览:150|全站可见|转载
 

下一篇:

上一篇:

 
删除

cdhigh:为什么那么麻烦?
建议标准库shutil

核心功能一个语句搞定:

shutil.copytree(src, dst)

2020-02-15 21:37 -

朝朝暮暮:你好,谢谢你给出建议。我是一个python的新手,有好多的知识没有学到呢

2020-02-16 12:29

Powered by phpwind v8.7 Certificate Copyright Time now is:10-31 03:02
©2005-2016 QTCN开发网 版权所有 Gzip disabled