PyQt5-从QLineEdit传递用户输入以更新另一个文件中的字典

问题描述 投票:0回答:1

form.py中,我有一个Form类,该类允许用户输入各种数据。想法是使用此数据更新document.py中字典中的值,然后将其用于填充pdf文件。通过主逻辑文件中的按钮调用创建该pdf的“ write custom pdf”方法。但是,下面的方法get_input_1(从表单导入到doc)根本无法更新字典。我尝试了各种选项,包括描述为here的解决方案,但它们似乎都不起作用。任何帮助将不胜感激!

gui.py

from PyQt5 import QtWidgets, QtCore

class UiMainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("File Manager")
        MainWindow.resize(1120, 750)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.button1 = QtWidgets.QPushButton(self.centralwidget)
        self.button1.setGeometry(QtCore.QRect(10, 80, 121, 41))

        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("File Manager", "File Manager"))
        self.button1.setText(_translate("MainWindow", "+ New File"))

main.py

from PyQt5 import QtWidgets, QtCore
from gui import UiMainWindow
from document import Doc
import sys

class Logic(QtWidgets.QMainWindow, UiMainWindow, Doc):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.button1.clicked.connect(self.write_custom_pdf)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    logic = Logic()
    logic.show()
    sys.exit(app.exec_())

form.py

from PyQt5.QtWidgets import *

class Form(QDialog):
    """Show a pop-up form for the user to input file data."""
    NumGridRows = 3
    NumButtons = 4

    def __init__(self, parent=None):
        super().__init__(parent)
        self.input_1 = QLineEdit(self)

        self.button_box = QDialogButtonBox(QDialogButtonBox.Ok |
                                       QDialogButtonBox.Cancel, self)
        self.button_box.accepted.connect(self.accept)
        self.button_box.rejected.connect(self.reject)

        self.formbox = QGroupBox("Please provide data below: ")
        layout = QFormLayout()
        layout.addRow(QLabel("Business name: "), self.input_1)
        layout.addRow(QLabel("Customer name: "), self.input_2)
        self.formbox.setLayout(layout) 

        main_layout = QVBoxLayout()
        main_layout.addWidget(self.formbox)
        main_layout.addWidget(self.button_box)
        self.setLayout(main_layout)

    def get_input_1(self):
        return self.input_1.text()

document.py

from PyQt5.QtWidgets import *
from form import Form
import os

class Doc(QInputDialog):
    """Create an doc and populate it based on user input."""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.work_dir = os.path.join("C:\\", "Document Manager", "Work environment")
        self.dialog = Form()
        self.template_path = 'invoice_template.pdf'
        input_1 = self.dialog.get_input_1()
        self.data_dict = {
            'business_name_1': f"{input_1}",
            'customer_name': 'company.io'}

        new_pdf = self.process_pdf(self.template_path)
        self.dialog.accepted.connect(lambda: self.produce_invoice(self.output_path, new_pdf))

    def write_custom_pdf(self):
        """Create an invoice based on a pdf template."""
        user_input = QInputDialog()
        active = True
        while active:
            text, ok = user_input.getText(self, "Invoice name", "Please enter a name")
            if ok and text != '':
                self.name = text
                self.output_path = f"{self.work_dir}" + "\\" + f"{self.name}" + ".pdf"
                if not os.path.exists(self.output_path):
                    self.open_dialog()
                    active = False
                else:
                    self.show_popup()
            else:
                active = False

    def open_dialog(self):
        self.dialog.exec_()

    def process_pdf(self, template_path):
        template_pdf = pdfrw.PdfReader(template_path)
        template_pdf.Root.AcroForm.update(
            pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
        annotations = template_pdf.pages[0][ANNOT_KEY]
        for annotation in annotations:
            if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
                if annotation[ANNOT_FIELD_KEY]:
                    key = annotation[ANNOT_FIELD_KEY][1:-1]
                    if key in self.data_dict.keys():
                        annotation.update(pdfrw.PdfDict(
                            V=f"{self.data_dict[key]}"))
        return template_pdf

    def produce_invoice(self, output_path, new_pdf):
        new_invoice = pdfrw.PdfWriter().write(output_path, new_pdf)
        return new_invoice
python pyqt5 qlineedit qdialog qformlayout
1个回答
0
投票

这不起作用,因为仅在__init__中更新了字典。get_input_1()函数不是某种“动态”的东西,它只是立即返回文本字段的[[current值,在创建表单时该值为空。

要更新字典,请调用函数

之后

对话框的exec()然后处理pdf。 def open_dialog(self): if self.dialog.exec_(): self.data_dict['business_name_1'] = self.dialog.get_input_1() newpdf = self.process_pdf(self.template_path) self.produce_invoice(self.output_path, newpdf)
© www.soinside.com 2019 - 2024. All rights reserved.