告诉我如何用这段代码正确写入数据
import csv
import datetime
import os
from random import randint
import openpyxl
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMessageBox
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1223, 578)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.QuestionField = QtWidgets.QTextBrowser(self.centralwidget)
self.QuestionField.setObjectName("QuestionField")
self.verticalLayout.addWidget(self.QuestionField)
self.EntryField = QtWidgets.QPlainTextEdit(self.centralwidget)
self.EntryField.setObjectName("EntryField")
self.verticalLayout.addWidget(self.EntryField)
self.ButtonNext = QtWidgets.QPushButton(self.centralwidget)
self.ButtonNext.setObjectName("ButtonNext")
self.verticalLayout.addWidget(self.ButtonNext)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Опросник"))
self.ButtonNext.setText(_translate("MainWindow", "Далее"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.question_list = [] # +++
self.wb_obj = openpyxl.load_workbook("questions.xlsx") # +++
self.ButtonNext.clicked.connect(self.get_next_question) # !!! +++
self.start_quiz()
self.get_next_question()
def get_next_question(self):
if len(self.question_list) < 3:
wb_sheet = self.wb_obj['Block_1']
elif len(self.question_list) == 3:
wb_sheet = self.wb_obj['Block_2']
elif len(self.question_list) == 4:
wb_sheet = self.wb_obj['Block_3']
else:
# sys.exit(app.exec_())
self.close()
answer = self.EntryField.toPlainText()
rows = wb_sheet.max_row
nums_wb = randint(2, rows)
question = wb_sheet[f'A{nums_wb}'].value
self.question_list.append(question)
self.QuestionField.setText(question)
self.write_to_file(question, answer)
print(question, '--', answer, '--', len(self.question_list))
self.EntryField.clear()
def write_to_file(self, question, answer):
with open(f"{os.getlogin()}_{datetime.date.today()}.csv", 'a',
encoding='cp1251', errors='replace', newline='') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow([question, answer])
def start_quiz(self):
greetings = QMessageBox()
greetings.setWindowTitle('Приветствие')
greetings.setText(
f'Приветствую тебя {os.getlogin()}. Предлогаю в начале рабочего дня пройти тебе тест на знание наших цен и предложений')
greetings.setIcon(QMessageBox.Information)
greetings.exec_()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
这是文件的链接,以便您可以检查它。在回答问题时,文件目前以
https://docs.google.com/spreadsheets/d/1WKymcPzKrgxBLLMYD5s5TTW9S3t5Drfn/edit?usp=share_link&ouid=109137112156684725929&rtpof=true&sd=true
问答的形式保存信息是必要的,
当你回答时,保存的结果尽可能奇怪第一个问题,答案转到刚刚创建的第二个问题,第一个仍然是空的
试试这样: