【发布时间】:2020-12-15 16:47:01
【问题描述】:
我正在尝试将用户在打开窗口提示符下的输入保存到 Excel 文件中。 我在 53-59 之间的代码行中尝试了一些东西,但它不起作用。你能帮我解决这个问题吗?
另外,在每一个新的用户条目中,新的信息都应该放在excel表格的最后一行。它不应覆盖以前的输入。
非常感谢
import sys
from PyQt5.QtWidgets import QDialogButtonBox
from PyQt5.QtWidgets import QFormLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import *
from xlwt import Workbook
class InputDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Information Window")
self.first = QLineEdit()
self.second = QLineEdit()
self.third = QLineEdit()
self.fourth = QLineEdit()
self.fifth = QLineEdit()
self.sixth = QLineEdit()
self.seventh = QLineEdit()
dlglayout = QVBoxLayout(self)
formlayout = QFormLayout()
formlayout.addRow("Fırst Name:", self.first)
formlayout.addRow("Second Name:", self.second)
formlayout.addRow("Age:", self.third )
formlayout.addRow("Sex:", self.fourth)
formlayout.addRow("Marital Status:", self.fifth)
formlayout.addRow("Education:", self.sixth)
formlayout.addRow("Job:", self.seventh)
dlglayout.addLayout(formlayout)
btns = QDialogButtonBox()
btns.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Save)
dlglayout.addWidget(btns)
btns.accepted.connect(self.accept)
btns.rejected.connect(self.reject)
def getInputs(self):
return self.first.text(), self.second.text(), self.third.text(),
self.fourth.text(), self.fifth.text(), self.sixth.text(), self.seventh.text()
wb = Workbook()
sheet1 = wb.add_sheet('Sheet 1')
sheet1.write(1, 0, 'First Name')
sheet1.write(2, 0, 'Second Name')
sheet1.write(3, 0, 'Age')
sheet1.write(4, 0, 'Sex')
sheet1.write(5, 0, 'Marital Status')
sheet1.write(6, 0, 'Education')
sheet1.write(7, 0, 'Job:')
sheet1.write(0, 1, 'self.first')
sheet1.write(0, 2, 'self.second')
sheet1.write(0, 3, 'self.third')
sheet1.write(0, 4, 'self.fourth')
sheet1.write(0, 5, 'self.fifth')
sheet1.write(0, 6, 'self.sixth')
sheet1.write(0, 7, 'self.seventh')
wb.save('output example.xls')
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = InputDialog()
if dialog.exec():
print(dialog.getInputs())
exit(0)
【问题讨论】:
-
xlwt 库适用于较旧的二进制 excel 文件 (Office 2003),并且只写入文件。这是你想要的吗?
-
如果你运行代码,你会看到弹出一个窗口,输入你要求的信息后,它会将信息保存到excel表中。但它现在不能正常工作,我想修复它。顺便说一句,如果有比 xlwt 更新的包,我可以用更新的包替换它。
-
请说的更清楚些。 “它不能正常工作”对我们没有任何意义。我只能假设问题是保存的文件有“self.first”、“self.second”等。不要让我们“跑来看”(例如,我不能用xlwt),给我们解释一下。
-
:) 好的,我会尽力的。问题是;保存的 excel 文件不显示您输入的输入。它显示 ("self.first", "self.second".... "self.seventh") 而不是输入。但通常,它应该显示“您输入的名字”、“您输入的第二个名字”、...“您输入的工作”。我希望你现在明白了,谢谢
标签: python python-3.x pyqt pyqt5 pyqt4