为了获得更多使用 Python 和创建 GUI 的实践经验,我决定创建一个抽认卡测验应用程序。最初,我创建了一个接受 csv 文件名的简单函数,将答案和问题打乱,并实现了一个for
循环来要求用户为每个问题输入答案。我通过 CMD 运行它,它运行良好。
接下来,我使用 Qt 设计器创建了一个简单的 Qt Dialog 窗口,带有一个textBrowser
用于输出和一个lineEdit
用于输入。(旁注:我知道你不应该修改生成的 ui 文件,所以我复制了代码并将其保存到不同的目录中,以便我可以安全地使用它。)我将测验功能放在 Dialog 类中,并让它调用应用程序的执行。但是,为了等待用户输入输入,我需要QEventLoop
在提出问题后启动并在lineEdit.returnPressed
触发时退出的测验功能中添加一个。如果我循环浏览整副纸牌,洗牌功能就会完成,当我关闭 GUI(通过 X 按钮)时,代码会定期停止。但是,如果我尝试关闭一个问题被问到和被回答之间的窗口(而QEventLoop
正在运行),GUI 关闭但功能仍在运行,并且aboutToQuit
我设置的检测器没有被触发。
我很确定这个问题是因为测验功能QEventLoop
在QEventLoop
执行. 让窗口和QEventLoop
运行同步解决我的问题吗?QEventLoop
在函数的窗口关闭等事件的情况下,有没有办法过早地突破?还是我应该使用像QTimer
这里这样的不同流程?
# If this helps, here's the code for the program.
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
import csv
import random
import sys
class Ui_Dialog(QWidget):
loop = QtCore.QEventLoop()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(361, 163)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(20, 120, 321, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.returnPressed.connect(self.acceptText)
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(20, 20, 321, 91))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
def printText(self, contents):
self.textBrowser.append(contents)
def acceptText(self):
input = self.lineEdit.text().strip().lower()
self.loop.quit()
return input
def shuffleDeck(self, filename):
# sets up values to be referenced later
questions = []
answers = []
index = 0
numright = 0
# contains the entire reading, shuffling, and quizzing process
with open(filename, encoding='utf-8') as tsv:
reader = csv.reader(tsv, delimiter="\t")
for row in reader:
questions.append(row[0][::-1])
answers.append(row[1].lower())
seed = random.random()
random.seed(seed)
random.shuffle(questions)
random.seed(seed)
random.shuffle(answers)
for question in questions:
# handles input & output
self.printText("What does " + question + " mean?")
self.loop.exec_()
guess = self.acceptText()
self.textBrowser.append(guess)
self.lineEdit.clear()
# compares input to answer, returns correct/incorrect prompts accordingly
if guess == answers[index]:
self.printText("You are right!")
index += 1
numright += 1
else:
self.printText("You are wrong. The answer is " + str(answers[index]) + "; better luck next time!")
index += 1
self.printText("You got " + str(round(numright / len(questions), 2) * 100) + "% (" + str(
numright) + ") of the cards right.")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
# I temporarily hardcoded a csv file
ui.shuffleDeck("Decks/Animals.csv")
# linear processing requires shuffleDeck to be completed before the window loops, right?
sys.exit(app.exec_())
#An example of the csv text would be:
מאוד VERY
עוד MORE
כמו כ AS
שם THERE
#I would have included only English characters, but this is the deck that I hardcoded in.