python大作业之单词记忆测试系统
采用pyqt5界面开发,并从csv文件中随机获取20个单词
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import pandas as pd
import random
global right_num
global spend_time
global word_list
right_num=0
spend_time=0
sty='''
QWidget {
/* Set all widgets to be centered */
qproperty-alignment: AlignCenter;
}
QLabel {
/* Set the label font size and color */
font-size: 36px;
color: blue;
/* Set the label text shadow */
text-shadow: 0px 0px 5px white;
}
QPushButton {
/* Set the button font size and color */
font-size: 24px;
color: white;
/* Set the button background gradient */
background: qradialgradient(cx:0.5, cy:0.5, radius:1,
fx:0.5, fy:0.5,
stop:0 #00ffff, stop:0.5 #008080, stop:1 #00ffff);
/* Set the button border and border radius */
border: 3px solid black;
border-radius: 10px;
/* Set the button box shadow */
box-shadow: inset -5px -5px 10px rgba(0,0,0,0.5),
inset 5px 5px 10px rgba(255,255,255,0.5);
}
QPushButton:hover {
/* Set the button background gradient when hovered */
background: qradialgradient(cx:0.5, cy:0.5, radius:1,
fx:0.5, fy:0.5,
stop:0 #ff00ff, stop:0.5 #800080, stop:1 #ff00ff);
}
QLineEdit {
border: 2px solid blue;
border-radius: 5px;
background-color: lightblue;
}
QLineEdit:hover {
border-color: green;
background-color: lightgreen;
}
QLineEdit:read-only {
border-color: gray;
background-color: lightgray;
}
'''
def getwords():
# 读取数据集
df = pd.read_csv("EnWords.csv")
# 获取单词列
words = df["word"]
global word_list
# 生成一个20个单词的随机列表
word_list = random.sample(list(words), 20)
class Memory(QWidget):
def __init__(self):
self.head=None
self.start=None
self.end=None
self.mainWindow=None
super(Memory,self).__init__()
self.setup_ui()
def setup_ui(self):
self.resize(600,400)
self.setWindowTitle("单词记忆测试系统")
self.setWindowIcon(QIcon("cover.jfif"))
self.setStyleSheet(sty)
box=QVBoxLayout(self)
box.setSpacing(40)
head=QLabel(self)
head.setText("单词记忆测试系统")
#开始按钮
start=QPushButton(self)
start.setText("开始")
#结束按钮
end=QPushButton(self)
end.setText("关闭系统")
self.head=head
self.start=start
self.end=end
start.clicked.connect(self.open_mainwindow)
end.clicked.connect(self.my_close)
box.addWidget(head)
box.addWidget(start)
box.addWidget(end)
def open_mainwindow(self):
self.mainWindow=MainWindow()
self.mainWindow.show()
self.close()
def my_close(self):
box=QMessageBox()
box.setWindowIcon(QIcon("img.png"))
box.setWindowTitle("关闭确认")
box.setText("是否要关闭系统?")
box.setStandardButtons(QMessageBox.Yes|QMessageBox.No)
Y=box.button(QMessageBox.Yes)
Y.setText("确定")
N=box.button(QMessageBox.No)
N.setText("取消")
box.exec_()
if box.clickedButton() == Y:
self.close()
else:
pass
class MainWindow(QWidget):
def __init__(self):
self.grid=None
self.memoryTest=None
self.exam=None
global right_num
global spend_time
right_num=0
spend_time=0
super(MainWindow,self).__init__()
self.setup_ui()
def setup_ui(self):
self.resize(800,700)
self.setWindowTitle("单词记忆测试系统")
self.setWindowIcon(QIcon("cover.jfif"))
self.setStyleSheet(sty)
grid=QGridLayout()
grid.setSpacing(20)
box = QVBoxLayout(self)
button1=QPushButton(self)
button1.setText("更新单词")
button1.clicked.connect(self.setup_words)
button2=QPushButton(self)
button2.setText("开始测试")
button3=QPushButton(self)
button3.setText("回到主界面")
button2.clicked.connect(self.start_exam)
button3.clicked.connect(self.back_memory)
box.addLayout(grid)
box.addWidget(button1)
box.addWidget(button2)
box.addWidget(button3)
self.grid=grid
self.setup_words()
def start_exam(self):
self.exam=Exam()
self.exam.show()
self.close()
def setup_words(self):
getwords()
num=0
for i in range(5):
for j in range(4):
if self.grid.itemAtPosition(i,j):
self.grid.itemAtPosition(i,j).widget().deleteLater()
word=QLabel("%d:%s"%(i*4+j+1,word_list[num]))
num=num+1
self.grid.addWidget(word,i,j,1,1)
def back_memory(self):
box = QMessageBox()
box.setWindowIcon(QIcon("img.png"))
box.setWindowTitle("关闭确认")
box.setText("是否回到主界面?")
box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
Y = box.button(QMessageBox.Yes)
Y.setText("确定")
N = box.button(QMessageBox.No)
N.setText("取消")
box.exec_()
if box.clickedButton() == Y:
self.memoryTest=Memory()
self.memoryTest.show()
self.close()
else:
pass
class Exam(QWidget):
def __init__(self):
self.form=None
self.word_num=None
self.word=None
self.rnum=0
self.num=0
self.count = 0
self.timecont=None
self.res=None
super(Exam,self).__init__()
self.setup_ui()
def setup_ui(self):
self.resize(400,200)
self.setWindowTitle("单词记忆测试系统")
self.setWindowIcon(QIcon("cover.jfif"))
self.setStyleSheet(sty)
form=QFormLayout(self)
timecont=QLabel(self)
timer=QTimer(self)
timer.start(1000)
timecont.setText("开始计时!")
timer.timeout.connect(self.update_count)
form.addRow(timecont)
word_num=QLabel(self)
word_num.setText("第%d个单词:"%(self.num+1))
word=QLineEdit(self)
word.setPlaceholderText("请输入答案")
form.addRow(word_num,word)
sub=QPushButton(self)
sub.setText("确定")
complete=QPushButton("完成测试")
sub.clicked.connect(self.check_word)
complete.clicked.connect(self.start_res)
form.addRow(sub)
form.addRow(complete)
self.word=word
self.word_num=word_num
self.timecont=timecont
def check_word(self):
i=self.num
answer=word_list[i]
if answer == self.word.text():
Y=QMessageBox.information(self,"回答结果","恭喜你答对了!",QMessageBox.Yes)
self.rnum=self.rnum+1
global right_num
right_num=self.rnum
else:
N=QMessageBox.information(self,"回答结果","很遗憾,正确答案为%s"%(word_list[i]),QMessageBox.Yes)
self.num=i+1
self.word_num.setText("第%d个单词:"%(self.num+1))
self.word.setText("")
if self.num >= 20:
self.res = Res()
self.res.show()
self.close()
def update_count(self):
# 更新计数值,每次加1
self.count += 1
global spend_time
spend_time=self.count
h=self.count/3600
m=(self.count%60)/60
s=self.count%3600
# 在标签上显示计数值
self.timecont.setText("用时 :%d时%d分%d秒"%(h,m,s))
def start_res(self):
self.res=Res()
self.res.show()
self.close()
class Res(QWidget):
def __init__(self):
self.mainwindow=None
self.memory=None
super(Res,self).__init__()
self.setup_ui()
def setup_ui(self):
self.resize(800, 700)
self.setWindowTitle("单词记忆测试系统")
self.setWindowIcon(QIcon("cover.jfif"))
self.setStyleSheet(sty)
box=QVBoxLayout(self)
box.setSpacing(30)
head=QLabel(self)
score=QLabel(self)
t=QLabel(self)
button1=QPushButton(self)
button2=QPushButton(self)
head.setText("您的成绩单")
global right_num
global spend_time
r=right_num
st=spend_time
h = st / 3600
m = (st % 60) / 60
s = st % 60
score.setText("正确的个数:%d"%r)
t.setText("时间:%d时%d分%d秒"%(h,m,s))
button1.setText("再次测试")
button2.setText("回到主界面")
button1.clicked.connect(self.back_mainwindow)
button2.clicked.connect(self.back_memory)
box.addWidget(head)
box.addWidget(score)
box.addWidget(t)
box.addWidget(button1)
box.addWidget(button2)
def back_memory(self):
self.memory=Memory()
self.memory.show()
self.close()
def back_mainwindow(self):
self.mainwindow=MainWindow()
self.mainwindow.show()
self.close()
if __name__ == '__main__':
app=QApplication(sys.argv)
test=Memory()
test.show()
sys.exit(app.exec_())