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

Posted

技术标签:

【中文标题】PyQt5 - 从 QLineEdit 传递用户输入以更新另一个文件中的字典【英文标题】:PyQt5 - Passing user input from QLineEdit to update a dictionary in another file 【发布时间】:2020-03-26 13:34:48 【问题描述】:

form.py 我有一个 Form 类,它允许用户输入各种数据。 这个想法是使用这些数据来更新 document.py 中字典中的值,然后将其用于填充 pdf 文件。通过我的主逻辑文件中的按钮调用创建所述 pdf 的“编写自定义 pdf”方法。 但是,下面的方法 get_input_1(从表单导入到文档)根本无法更新字典。我尝试了各种选项,包括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"))

ma​​in.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

【问题讨论】:

提供minimal reproducible example 我已经添加了必要的组件 【参考方案1】:

它不起作用,因为字典只在__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)

【讨论】:

它工作了 :) 也谢谢你的解释,我觉得这与初始化有关,但无法弄清楚。绝对的传奇,非常感谢!

以上是关于PyQt5 - 从 QLineEdit 传递用户输入以更新另一个文件中的字典的主要内容,如果未能解决你的问题,请参考以下文章

如何使 Pyqt5 QLineEdit 只接受数字

PYQT5 qlineedit 隐藏后不显示

如何从 QLineEdit 访问值?

PyQT5 显示在窗口上花费的时间

Python PyQt5:如果 QLineEdit 为空,如何更改 QLabel 的颜色?

《PyQT5软件开发 - 控件篇》第3章 单行文本框QLineEdit