PyQt5 - 从 QLineEdit 传递用户输入以更新另一个文件中的字典
PyQt5 - Passing user input from QLineEdit to update a dictionary in another file
在form.py中我有一个表单class,它允许用户输入各种数据。
这个想法是使用此数据更新 document.py 中字典中的值,然后将其用于填充 pdf 文件。创建所述 pdf 的 "write custom pdf" 方法是通过我的主逻辑文件中的按钮调用的。
但是,下面的方法get_input_1(从表单导入到文档)根本无法更新字典。我尝试了各种选项,包括 中描述的解决方案,但其中 none 似乎有效。任何帮助将不胜感激!
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
它不起作用,因为字典仅在 __init__
中更新。
get_input_1()
函数不是某种 "dynamic" 东西,它只是立即 returns 文本字段的 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)
在form.py中我有一个表单class,它允许用户输入各种数据。
这个想法是使用此数据更新 document.py 中字典中的值,然后将其用于填充 pdf 文件。创建所述 pdf 的 "write custom pdf" 方法是通过我的主逻辑文件中的按钮调用的。
但是,下面的方法get_input_1(从表单导入到文档)根本无法更新字典。我尝试了各种选项,包括
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
它不起作用,因为字典仅在 __init__
中更新。
get_input_1()
函数不是某种 "dynamic" 东西,它只是立即 returns 文本字段的 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)