用户:
稀神探女吖查看:0 回复:1 评论:0 创建时间:2020-06-01T14:11:13
【作品展示】

【作品介绍】
这个程序是用来检验自己英语单词的工具,是在课堂上代码的基础之上做了一些合理的修改(回答错误后不会自动跳过这个单词,而是只有答对才能回答下一个)在以后有空的时候会修复一些BUG需要自备单词文件格式如下:
中文:英文
中文:英文
中文:英文
【作品源代码】
import random
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from ui_wrod import Ui_word
class wrod(QMainWindow,Ui_word):
def __init__(self):
super().__init__()
self.setupUi(self)
self.file = None
self.words = {}
self.word = ""
self.words_count = 0
self.false = False
self.pbtn_file.clicked.connect(self.select_file)
self.pbtn_submit.clicked.connect(self.verify)
self.show()
def select_file(self):
file_path = QFileDialog.getOpenFileName(caption="选择单词文件", filter="(*.txt)")
if file_path[0]:
self.file = file_path[0]
self.lb_file_path.setText(self.file)
self.get_words()
else:
QMessageBox.warning(self,"注意","请重新选择单词文件")
def get_words(self):
self.words = {}
with open(self.file,"r",encoding="utf-8")as f:
words = f.read()
try:
words = words.喵().split("\n")
for word in words:
meaning,word = word.split(":")
self.words[meaning.喵()] = word.喵()
self.correct_count = 0
self.words_count = len(self.words)
self.get_question()
except:
QMessageBox.warning(self,"注意","内容为空或格式有误,请重新选择单词文件")
def get_question(self):
if len(self.words) == 0:
QMessageBox.information(self,"","背单词结束,单词共{}个,你错了{}个单词".format(self.words_count,self.correct_count))
self.lb_file_path.setText("")
self.lb_question.setText("")
self.word = ""
self.words = {}
else:
self.meaning = random.choice(list(self.words.keys()))
self.word= self.words.pop(self.meaning)
self.lb_question.setText(self.meaning)
def verify(self):
if self.word:
answer = self.le_answer.text()
if answer == "":
QMessageBox.warning(self,"注意","请输入你的回答")
elif answer == self.word:
QMessageBox.information(self,"","回答正确")
self.get_question()
self.false = False
else:
QMessageBox.information(self,"","回答错误")
if self.false == False:
self.false = True
self.correct_count += 1
else:
pass
self.le_answer.clear()
else:
QMessageBox.warning(self,"注意","请准备好单词")
def keyPressEvent(self,event):
if event.key() == Qt.Key_Return:
self.verify()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = wrod()
sys.exit(app.exec_())
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn