通过Pyqt5 python保存文件[重复]

Posted

技术标签:

【中文标题】通过Pyqt5 python保存文件[重复]【英文标题】:saving a file via Pyqt5 python [duplicate] 【发布时间】:2019-03-02 15:06:32 【问题描述】:

我已经阅读了几个小时认为该网站并且无法得到正确答案,请帮助我解决我的问题,我仍处于编程的开始步骤:)

我想写一个保存文件的程序! (作为锻炼的一部分。) 我可以获取文件并可以应用地址,但该过程未应用,我知道在某个地方我必须将路径类转换为 str,但我做不到。我使用 QT Designer for Pyqt5 并且我还包括我的 ui 文件。


from PyQt5.QtWidgets import QApplication, QFileDialog
from PyQt5.uic import loadUi
import sys

def get ():
    QFileDialog.getOpenFileNames()
    return

def paste ():
    QFileDialog.getSaveFileName()
    return

def saver ():
    global temp
    global destination
    with open(destination,'w')as file:
        file.write(temp)


if __name__ == '__main__':
    temp = ''
    destination = ''
    app = QApplication(sys.argv)
    x=loadUi('save.ui')
    temp=x.pushButton_2.clicked.connect(get)
    address= x.pushButton_3.clicked.connect(paste)
    x.pushButton.clicked.connect (saver)
    x.show()
    sys.exit(app.exec())

我在加载部分用作 save.ui 的 Qt 设计器生成的 UI 文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>223</width>
    <height>112</height>
   </rect>
  </property>
  <property name="palette">
   <palette>
    <active>
     <colorrole role="WindowText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Button">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>85</green>
        <blue>127</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Light">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>128</green>
        <blue>191</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Midlight">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>106</green>
        <blue>159</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Dark">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>42</green>
        <blue>63</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Mid">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>56</green>
        <blue>84</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Text">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="BrightText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ButtonText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Base">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Window">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>85</green>
        <blue>127</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Shadow">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="AlternateBase">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>42</green>
        <blue>63</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ToolTipBase">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>220</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ToolTipText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </colorrole>
    </active>
    <inactive>
     <colorrole role="WindowText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Button">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>85</green>
        <blue>127</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Light">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>128</green>
        <blue>191</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Midlight">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>106</green>
        <blue>159</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Dark">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>42</green>
        <blue>63</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Mid">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>56</green>
        <blue>84</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Text">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="BrightText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ButtonText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Base">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Window">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>85</green>
        <blue>127</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Shadow">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="AlternateBase">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>42</green>
        <blue>63</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ToolTipBase">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>220</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ToolTipText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </colorrole>
    </inactive>
    <disabled>
     <colorrole role="WindowText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>42</green>
        <blue>63</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Button">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>85</green>
        <blue>127</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Light">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>128</green>
        <blue>191</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Midlight">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>106</green>
        <blue>159</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Dark">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>42</green>
        <blue>63</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Mid">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>56</green>
        <blue>84</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Text">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>42</green>
        <blue>63</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="BrightText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ButtonText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>42</green>
        <blue>63</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Base">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>85</green>
        <blue>127</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Window">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>85</green>
        <blue>127</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="Shadow">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="AlternateBase">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>85</green>
        <blue>127</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ToolTipBase">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>220</blue>
       </color>
      </brush>
     </colorrole>
     <colorrole role="ToolTipText">
      <brush brushstyle="SolidPattern">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </colorrole>
    </disabled>
   </palette>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QPushButton" name="pushButton_2">
      <property name="font">
       <font>
        <family>MS Shell Dlg 2</family>
        <pointsize>9</pointsize>
        <weight>9</weight>
        <italic>false</italic>
        <bold>false</bold>
       </font>
      </property>
      <property name="styleSheet">
       <string notr="true">color: rgb(0, 0, 0);
background-color: rgb(255, 170, 0);
font: 75 9pt &quot;MS Shell Dlg 2&quot;;</string>
      </property>
      <property name="text">
       <string>Choose a file to copy</string>
      </property>
      <property name="autoDefault">
       <bool>false</bool>
      </property>
     </widget>
    </item>
    <item row="3" column="0">
     <widget class="QProgressBar" name="progressBar">
      <property name="styleSheet">
       <string notr="true">color: rgb(255, 255, 255);</string>
      </property>
      <property name="value">
       <number>24</number>
      </property>
     </widget>
    </item>
    <item row="2" column="0">
     <widget class="QPushButton" name="pushButton">
      <property name="styleSheet">
       <string notr="true">color: rgb(0, 0, 0);
background-color: rgb(255, 170, 0);
font: 75 9pt &quot;MS Shell Dlg 2&quot;;</string>
      </property>
      <property name="text">
       <string>COPY</string>
      </property>
      <property name="autoDefault">
       <bool>false</bool>
      </property>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QPushButton" name="pushButton_3">
      <property name="font">
       <font>
        <family>MS Shell Dlg 2</family>
        <pointsize>9</pointsize>
        <weight>9</weight>
        <italic>false</italic>
        <bold>false</bold>
       </font>
      </property>
      <property name="styleSheet">
       <string notr="true">color: rgb(0, 0, 0);
background-color: rgb(255, 170, 0);
font: 75 9pt &quot;MS Shell Dlg 2&quot;;</string>
      </property>
      <property name="text">
       <string>Choose a place to paste</string>
      </property>
      <property name="autoDefault">
       <bool>false</bool>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

【参考方案1】:

试试看:

import sys
from shutil import copyfile                                         # <---
from PyQt5.QtWidgets import QApplication, QFileDialog, QMainWindow
from PyQt5.uic import loadUi


class CopyFiles(QMainWindow):
    def __init__(self):
        super().__init__()

        loadUi('save.ui', self)

        self.progressBar.setValue(0)
        self.fileNames    = ""
        self.saveFileName = ""

        self.pushButton_2.clicked.connect(self.get)
        self.pushButton_3.clicked.connect(self.paste)
        self.pushButton.clicked.connect(self.saver)

    def get(self):
        self.fileName, _ = QFileDialog.getOpenFileName()
        print("fileNames", self.fileName)

    def paste(self):
        self.saveFileName, _ = QFileDialog.getSaveFileName()
        print("saveFileName", self.saveFileName)

    def saver(self):
        if self.saveFileName and self.fileName:
            copyfile(self.fileName, self.saveFileName)       
            self.progressBar.setValue(100)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = CopyFiles()
    w.show()
    sys.exit(app.exec())

【讨论】:

以上是关于通过Pyqt5 python保存文件[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 PyQt5 保存任何文件?

在 Pyqt5 中上传和保存文件

pyqt5读取psd文件

Python:如何保存 os.system 的输出 [重复]

PyQt5显示日期选择框,获取日期保存文件

使用 PyQT5 和 Python 的字符串格式化问题 [重复]