我在小程序中将两个函数相互连接有问题。 我正在使用PyQt5和PyPDF2模块。我想将两个pdf文件链接到一个。 在我的程序中,我有3个按钮:
函数单独工作但是当我想通过单击“开始”(btnPlik3)连接它们时,我的程序崩溃了。以下是我的代码:
def openFile1(self):
pathFileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, 'Wybierz plik', '', 'pdf(*.pdf)')
print("PathFileName-'{}', \n_-'{}'".format(pathFileName, _))
if pathFileName:
print("Wybrany plik: ", pathFileName)
g = open(pathFileName, 'rb')
return g
def openFile2(self):
pathFileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, 'Wybierz plik', '', 'pdf(*.pdf)')
print("PathFileName-'{}', \n_-'{}'".format(pathFileName, _))
if pathFileName:
print("Wybrany plik: ", pathFileName)
h = open(pathFileName, 'rb')
return h
def laczeniePdf(self,g, h):
readerLinkPage1 = PyPDF2.PdfFileReader(open(g, 'rb'))
readerLinkPage2 = PyPDF2.PdfFileReader(open(h, 'rb'))
writerLinkPage = PyPDF2.PdfFileWriter()
OutputFile = open('FinalOutput.pdf', 'wb')
writerLinkPage.appendPagesFromReader(readerLinkPage1)
writerLinkPage.appendPagesFromReader(readerLinkPage2)
writerLinkPage.write(OutputFile)
OutputFile.close()
我期待你的提示和建议
编辑:
Here is code of class we are talking about(it is separate window in my program)
class Ui_PolaczPliki(object):
def setupUi(self, PolaczPliki):
PolaczPliki.setObjectName("PolaczPliki")
PolaczPliki.resize(295, 113)
self.btnPlik1 = QtWidgets.QPushButton(PolaczPliki)
self.btnPlik1.setGeometry(QtCore.QRect(30, 40, 91, 41))
self.btnPlik1.setObjectName("btnPlik1")
self.btnPlik1.clicked.connect(self.openFile1)
self.btnPlik2 = QtWidgets.QPushButton(PolaczPliki)
self.btnPlik2.setGeometry(QtCore.QRect(160, 40, 91, 41))
self.btnPlik2.setObjectName("btnPlik2")
self.btnPlik2.clicked.connect(self.openFile2)
self.btnPlik3 = QtWidgets.QPushButton(PolaczPliki)
self.btnPlik3.setGeometry(QtCore.QRect(80, 80, 40, 20))
self.btnPlik3.setObjectName("btnPlik3")
self.btnPlik3.clicked.connect(self.laczeniePdf)
self.label = QtWidgets.QLabel(PolaczPliki)
self.label.setGeometry(QtCore.QRect(50, 10, 47, 13))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(PolaczPliki)
self.label_2.setGeometry(QtCore.QRect(180, 10, 47, 13))
self.label_2.setObjectName("label_2")
self.retranslateUi(PolaczPliki)
QtCore.QMetaObject.connectSlotsByName(PolaczPliki)
def retranslateUi(self, PolaczPliki):
_translate = QtCore.QCoreApplication.translate
PolaczPliki.setWindowTitle(_translate("PolaczPliki", "Polacz pliki"))
self.btnPlik1.setText(_translate("PolaczPliki", "Dodaj"))
self.btnPlik2.setText(_translate("PolaczPliki", "Dodaj"))
self.btnPlik3.setText(_translate("PolaczPliki", "Start"))
self.label.setText(_translate("PolaczPliki", "Plik nr 1"))
self.label_2.setText(_translate("PolaczPliki", "Plik nr 2"))
def openFile1(self):
pathFileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, 'Wybierz plik', '', 'pdf(*.pdf)')
print("PathFileName-'{}', \n_-'{}'".format(pathFileName, _))
if pathFileName:
print("Wybrany plik: ", pathFileName)
g = open(pathFileName, 'rb')
return g
def openFile2(self):
pathFileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, 'Wybierz plik', '', 'pdf(*.pdf)')
print("PathFileName-'{}', \n_-'{}'".format(pathFileName, _))
if pathFileName:
print("Wybrany plik: ", pathFileName)
h = open(pathFileName, 'rb')
return h
def laczeniePdf(self, g,h):
readerLinkPage1 = PyPDF2.PdfFileReader(g)
readerLinkPage2 = PyPDF2.PdfFileReader(h)
writerLinkPage = PyPDF2.PdfFileWriter()
OutputFile = open('FinalOutput.pdf', 'wb')
writerLinkPage.appendPagesFromReader(readerLinkPage1)
writerLinkPage.appendPagesFromReader(readerLinkPage2)
writerLinkPage.write(OutputFile)
OutputFile.close()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
试一试:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import PyPDF2
class Ui_PolaczPliki(object):
def __init__(self): # +++
self.file_1 = None # +++
self.file_2 = None # +++
def setupUi(self, PolaczPliki):
PolaczPliki.setObjectName("PolaczPliki")
PolaczPliki.resize(600, 150)
self.btnPlik1 = QtWidgets.QPushButton(PolaczPliki)
self.btnPlik1.setGeometry(QtCore.QRect(30, 40, 91, 41))
self.btnPlik1.setObjectName("btnPlik1")
self.btnPlik1.clicked.connect(self.openFile1)
self.btnPlik2 = QtWidgets.QPushButton(PolaczPliki)
self.btnPlik2.setGeometry(QtCore.QRect(300, 40, 91, 41))
self.btnPlik2.setObjectName("btnPlik2")
self.btnPlik2.clicked.connect(self.openFile2)
self.btnPlik3 = QtWidgets.QPushButton(PolaczPliki)
self.btnPlik3.setGeometry(QtCore.QRect(165, 90, 91, 41))
self.btnPlik3.setObjectName("btnPlik3")
self.btnPlik3.clicked.connect(lambda: self.laczeniePdf(self.file_1, self.file_2)) # +++
self.label = QtWidgets.QLabel(PolaczPliki)
self.label.setGeometry(QtCore.QRect(30, 10, 47, 13))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(PolaczPliki)
self.label_2.setGeometry(QtCore.QRect(300, 10, 47, 13))
self.label_2.setObjectName("label_2")
self.retranslateUi(PolaczPliki)
QtCore.QMetaObject.connectSlotsByName(PolaczPliki)
def retranslateUi(self, PolaczPliki):
_translate = QtCore.QCoreApplication.translate
PolaczPliki.setWindowTitle(_translate("PolaczPliki", "Polacz pliki"))
self.btnPlik1.setText(_translate("PolaczPliki", "Dodaj Plik nr 1"))
self.btnPlik2.setText(_translate("PolaczPliki", "Dodaj Plik nr 2"))
self.btnPlik3.setText(_translate("PolaczPliki", "Start"))
self.label.setText(_translate("PolaczPliki", "Plik nr 1"))
self.label_2.setText(_translate("PolaczPliki", "Plik nr 2"))
def openFile1(self):
pathFileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, 'Wybierz plik', '', 'pdf(*.pdf)')
print("PathFileName-'{}', \n_-'{}'".format(pathFileName, _))
if pathFileName:
print("\nWybrany plik: ", pathFileName)
self.file_1 = pathFileName # +++
self.label.setText("{}".format(self.file_1)) # +++
self.label.adjustSize() # +++
#g = open(pathFileName, 'rb')
#print("\ng = open(pathFileName, 'rb') =`{}`, \ntype g =`{}` ".format(g, type(g)))
#return g
def openFile2(self):
pathFileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, 'Wybierz plik', '', 'pdf(*.pdf)')
print("PathFileName-'{}', \n_-'{}'".format(pathFileName, _))
if pathFileName:
print("\nWybrany plik: ", pathFileName)
self.file_2 = pathFileName # +++
self.label_2.setText("{}".format(self.file_2)) # +++
self.label_2.adjustSize() # +++
#h = open(pathFileName, 'rb')
#print("\nh = open(pathFileName, 'rb') =`{}`, \ntype h =`{}` ".format(h, type(h)))
#return h
def laczeniePdf(self, file_1, file_2): # +++
g = open(file_1, 'rb') # +++
h = open(file_2, 'rb') # +++
readerLinkPage1 = PyPDF2.PdfFileReader(g)
readerLinkPage2 = PyPDF2.PdfFileReader(h)
writerLinkPage = PyPDF2.PdfFileWriter()
OutputFile = open('FinalOutput.pdf', 'wb')
writerLinkPage.appendPagesFromReader(readerLinkPage1)
writerLinkPage.appendPagesFromReader(readerLinkPage2)
writerLinkPage.write(OutputFile)
OutputFile.close()
print("\n g=`{}` + h=`{}` !!!".format(file_1, file_2))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = Ui_PolaczPliki() #Ui_MainWindow() # +++
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())