【Pyqt5】实现小学三年级口算题生成器

  • Post author:
  • Post category:其他


寒假在家无聊,弟弟学校布置数学作业 : 每天五分钟数学题口算,并记录时间。如果在本子上出题,然后让弟弟做,30多天,估计要写一本子,感觉好累。一直想学pyqt,这次终于找到了动力,于是Pyqt边学边写,实现了基本的口算题和时间功能,现分享如下:

一:首先在QTdesigner下画类似界面如下,包括10个Vertical Layout,以及3个Button,一堆label:

二:其次,UI文件转成py文件(工具:PyUIC)

三:然后呢,我们在py文件里进行程序的编写:


1:时间模块:包括日期的显示和5min的倒计时,主要程序如下:

self.time1 = time.time()
self.time2 = time.time()
self.timer_result = QtCore.QTimer()
def time_result1(self):
self.time2 = time.time()
 t = 301 - int(self.time2 - self.time1)
 if t==1:
     reply = QMessageBox.question(self, 'Message',
                                     "Time is out!", QMessageBox.Yes)
     if reply == QMessageBox.Yes:
            self.panduan()
            quit()

 t_m = '0' + str(int(t / 60))
 t_s = str(int(t % 60))
 self.label_5.setText(t_m + ':' + t_s)
我们设置了两个时间实例,进行比对后得到时间差,依次进行时间的倒计时。时间到了后弹出消息框提示,然后quit退出界面。为显示日期,我们加上这句话:

self.label_2.setText(time.strftime("%Y-%m-%d ", time.localtime()))

2:开始:Go按钮

我们Go按钮的事件是开启定时器(500ms),调用timu(),我们随机1-4代表+=*/四则运算,我们对每一个进行随机生成,然后对两个操作数进行随机生成,鉴于三年级,加减包括3位数,而乘除只有三位数和一位数,大家可以根据自己情况修改:

def jishi(self):
    self.timer_result.start(500)#0.5s定时器
    self.time1 = time.time()
    self.timu()
    self.pushButton.setEnabled(False)
def timu(self):
    for i in range(10):
        self.fuhao[i]=random.randint(1,4)

    for i in range(10):
        if self.fuhao[i] == 1:
            # +
            s1=random.randint(1,999)
            s2=random.randint(1,999)
            self.xuhao1_num[i] = s1
            self.xuhao3_num[i] = s2
            self.settext_label(self.xuhao1[i],str(s1))
            self.settext_label(self.xuhao3[i], str(s2))
            self.settext_label(self.xuhao2[i],'+')

        if self.fuhao[i]==2:
            # -
            s1=random.randint(1,999)
            s2 =random.randint(1, s1)
            self.xuhao1_num[i]=s1
            self.xuhao3_num[i]=s2
            self.settext_label(self.xuhao1[i], str(s1))
            self.settext_label(self.xuhao3[i], str(s2))
            self.settext_label(self.xuhao2[i], '-')

        if self.fuhao[i]==3:
            # *
            s1 = random.randint(1, 999)
            s2 = random.randint(2, 9)
            self.xuhao1_num[i]=s1
            self.xuhao3_num[i]=s2
            self.settext_label(self.xuhao1[i], str(s1))
            self.settext_label(self.xuhao3[i], str(s2))
            self.settext_label(self.xuhao2[i], '×')

        if self.fuhao[i] == 4:
            # /
            s2 = random.randint(2, 9)
            s1=random.randint(1,111)
            s1=s2*s1
            self.xuhao1_num[i] = s1
            self.xuhao3_num[i] = s2
            self.settext_label(self.xuhao1[i], str(s1))
            self.settext_label(self.xuhao3[i], str(s2))
            self.settext_label(self.xuhao2[i], '÷')

3:提交按钮和退出按钮编写:

提交:我们写完后将结果提交,后台处理填写结果(获取每个的label和lineEdit的数值,然后进行计算,判断答题人的正确与否,正确与否存入result_label),关键程序如下:


self.pushButton_3.clicked.connect(self.close_file)#Exit按钮
self.pushButton_4.clicked.connect(self.panduan)#Submit按钮
def close_file(self):
    quit()#退出键只退出界面即可
def panduan(self):
    for i in range(10):
        s=self.gettext_line(self.xuhao4[i])
        self.result[i]=int(s)
    for i in range(10):
        if self.fuhao[i] == 1:
            # +
            if self.xuhao1_num[i] +self.xuhao3_num[i]!=self.result[i]:
                self.result_label[i]=1
        if self.fuhao[i]==2:
            # -
            if self.xuhao1_num[i] -self.xuhao3_num[i]!=self.result[i]:
                self.result_label[i]=1
        if self.fuhao[i]==3:
            # *
            if self.xuhao1_num[i] *self.xuhao3_num[i]!=self.result[i]:
                self.result_label[i]=1
        if self.fuhao[i] == 4:
            # /
            if self.xuhao1_num[i] /self.xuhao3_num[i]!=self.result[i]:
                self.result_label[i]=1
self.timu()

4:写入word文档:

我们将题目和结果以及正确与否写入word文档:

首先安装python-docx库:pip install pyqt-docx

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
import time
import random
import docx
self.file = docx.Document()#创建实例

self.file.add_paragraph('XXX寒假口算题目')
self.file.add_paragraph(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) #写入标题

#写入计算式子
for i in range(10):
    if self.fuhao[i] == 1:
        # +
        if self.result_label[i]==1:
            self.file.add_paragraph(str(self.xuhao1_num[i])+'+'+str(self.xuhao3_num[i])+'='+str(self.result[i])+'')
        else:
            self.file.add_paragraph(str(self.xuhao1_num[i]) + '+'+str(self.xuhao3_num[i]) + '=' + str(self.result[i]) + '')
    if self.fuhao[i] == 2:
        # -
        if self.result_label[i] == 1:
            self.file.add_paragraph(
                str(self.xuhao1_num[i]) + '-' + str(self.xuhao3_num[i]) + '=' + str(self.result[i]) + '')
        else:
            self.file.add_paragraph(
                str(self.xuhao1_num[i]) + '-' + str(self.xuhao3_num[i]) + '=' + str(self.result[i]) + '')

    if self.fuhao[i] == 3:
        # *
        if self.result_label[i] == 1:
            self.file.add_paragraph(
                str(self.xuhao1_num[i]) + '×' + str(self.xuhao3_num[i]) + '=' + str(self.result[i]) + '')
        else:
            self.file.add_paragraph(
                str(self.xuhao1_num[i]) + '×' + str(self.xuhao3_num[i]) + '=' + str(self.result[i]) + '')

    if self.fuhao[i] == 4:
        # /
        if self.result_label[i] == 1:
            self.file.add_paragraph(
                str(self.xuhao1_num[i]) + '÷' + str(self.xuhao3_num[i]) + '=' + str(self.result[i]) + '')
        else:
            self.file.add_paragraph(
                str(self.xuhao1_num[i]) + '÷' + str(self.xuhao3_num[i]) + '=' + str(self.result[i]) + '')

大功告成,完整程序后续会分享。还有很多不完善的,望指出,共同交流学习~






版权声明:本文为qq_32747775原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。