千纬数学app(读题版)制作和源码

前段时间我制作一个给小学生练习100以内加减乘除的安卓app:制作一个给小学生练习100以内四则运算的app-CSDN博客,后来发现没有发出声音,不能知道孩子的学习情况,为此我制作了一个有朗读题目的app,这样小学生会更加喜欢它。

一、生成声音文件和声音时长数据

# -*- coding: utf-8 -*-
"""
Created on Fri Feb  9 08:58:22 2024

@author: YBK
"""

import pyttsx3
import sqlite3
import wave

def get_wav_duration(file_path):
    with wave.open(file_path, 'r') as wav_file:
        frames = wav_file.getnframes()
        rate = wav_file.getframerate()
        duration = frames / float(rate)
        return round(duration,3)
''' 
#创建记录0~100数字的声音时长数据表   
conn = sqlite3.connect('D:\\bee\\qwsx\\src\\qwsx\\resources\\qwsx.db',timeout=10, check_same_thread=False)
c = conn.cursor()
# 创建TTS引擎对象
number = 0
while number <= 100:
    sql = 'INSERT INTO voice (number,sc) VALUES (?,0);'
    c.execute(sql, (number,))
    conn.commit()
    number = number + 1
    
c.close()
conn.close()    
'''
'''
#生成0~100的声音文件并获取播放时长到数据表
conn = sqlite3.connect('qwsx.db',timeout=10, check_same_thread=False)
c = conn.cursor()
engine = pyttsx3.init()
engine.setProperty('audio_format', 'wav')
engine.setProperty('volume', 1.2)
number = 0#1,1001,2001
while number <= 100:
    text = str(number)
    fi = './'+str(number)+'.wav'
    engine.save_to_file(text, fi)
    engine.runAndWait()
    duration = get_wav_duration(fi)
    print(f"The duration of the WAV file is: {duration} seconds.")
    query_sql1 = "update voice set sc = ? where number= ?"
    c.execute(query_sql1, (duration,number,))
    conn.commit()
    number = number + 1

input("please input any key to exit!")
c.close()
conn.close()
'''
#生成+-*/=等声音文件并获取播放时长
engine = pyttsx3.init()
engine.setProperty('audio_format', 'wav')
engine.setProperty('volume', 1.2)
text = '填符号'
fi = './tian.wav'
#加 0.92 减 0.948 乘 0.975 除 0.975 等于 1.161 多少 1.066 填符号 1.302
engine.save_to_file(text, fi)
engine.runAndWait()
duration = get_wav_duration(fi)
print(f"The duration of the WAV file is: {duration} seconds.")

二、更新原主程序

from functools import partial
import toga
from toga.style.pack import COLUMN, LEFT, RIGHT, ROW, Pack
import sqlite3
import random
from time import time,sleep
from pathlib import Path
import datetime
import shutil
import miniaudio
import threading
#导入必要的库

resources_folder = Path(__file__).joinpath("../resources/").resolve()
db_filepath = resources_folder.joinpath("qwsx.db")
#db_filepath = '/storage/emulated/0/Pictures/qwsx.db'
#上面是数据库的存放位置,生成手机app请参考我以前的文章
#oppo版 需要用电脑调试应删除下面5行
'''
if Path('/storage/emulated/0/Documents/qwsx.db').exists():
    db_filepath = '/storage/emulated/0/Documents/qwsx.db'
else:
    shutil.copy(db_filepath, "/storage/emulated/0/Documents/qwsx.db")
    db_filepath = '/storage/emulated/0/Documents/qwsx.db'
'''
names = globals()
nd = 0
hd = 0 #回答为0为出题,回答为1为1位数答题,2为2位数答题,3为3位数答题,4为符号答题
dan = 101 #初始答案
dan1 = 0
dan2 = 0
dan3 = 0
tjcs = ''
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
    return False
#判断是不是数字
def bfnum(number):
    conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
    c = conn.cursor()
    cursor = c.execute("SELECT sc from voice where number = ?;",(number,))
    row = cursor.fetchone()
    sc = row[0]
    c.close()
    conn.close()
    stream = miniaudio.stream_file(str(resources_folder.joinpath(str(number) + '.wav')))
    with miniaudio.PlaybackDevice() as device:
        device.start(stream)
        sleep(sc)
def bffh(fh):
    if fh == '+':
        stream = miniaudio.stream_file(str(resources_folder.joinpath('jia.wav')))
        with miniaudio.PlaybackDevice() as device:
            device.start(stream)
            sleep(0.92)
    if fh == '-':
        stream = miniaudio.stream_file(str(resources_folder.joinpath('jian.wav')))
        with miniaudio.PlaybackDevice() as device:
            device.start(stream)
            sleep(0.948)
    if fh == '*':
        stream = miniaudio.stream_file(str(resources_folder.joinpath('sheng.wav')))
        with miniaudio.PlaybackDevice() as device:
            device.start(stream)
            sleep(0.975)
    if fh == '/':
        stream = miniaudio.stream_file(str(resources_folder.joinpath('chu.wav')))
        with miniaudio.PlaybackDevice() as device:
            device.start(stream)
            sleep(0.975)
    if fh == '□':
        stream = miniaudio.stream_file(str(resources_folder.joinpath('duo.wav')))
        with miniaudio.PlaybackDevice() as device:
            device.start(stream)
            sleep(1.066)
    if fh == '=':
        stream = miniaudio.stream_file(str(resources_folder.joinpath('deng.wav')))
        with miniaudio.PlaybackDevice() as device:
            device.start(stream)
            sleep(0.975)
    if fh == 'fh':
        stream = miniaudio.stream_file(str(resources_folder.joinpath('tian.wav')))
        with miniaudio.PlaybackDevice() as device:
            device.start(stream)
            sleep(0.975)
def bfsz(t1,tfh1,t2,tfh2,tdan):
    if is_number(t1):
        pnum(t1)
        sleep(0.65)
    elif t1 == '□':
        pfh('□')
        sleep(0.6)
    pfh(tfh1)
    sleep(0.6)
    if is_number(t2):
        pnum(t2)
        sleep(0.65)
    elif t2 == '□':
        pfh('□')
        sleep(0.6)
    pfh(tfh2)
    sleep(0.5)
    if is_number(tdan):
        pnum(tdan)
        sleep(0.5)
    elif tdan == '□':
        pfh('□')
        sleep(0.5)
def settm(nd):
    conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
    c = conn.cursor()
    if nd == 1:
        cursor = c.execute("SELECT * from ys where fh = 0 or fh = 1 ORDER BY RANDOM() LIMIT 1;")
        row = cursor.fetchone()
        tid = row[0]
        s1 = row[1]
        jj = row[2]
        s2 = row[3]
        dan = row[4]
    else:
        #按数据库乘法除法只有600题,加减法各5151题,如果随机经常是加减法,按25%概率强制出现乘法除法题
        if random.randint(0,3) == 3:
            cursor = c.execute("SELECT * from ys where fh = 2 or fh = 3 ORDER BY RANDOM() LIMIT 1;")
        else:
            cursor = c.execute("SELECT * from ys ORDER BY RANDOM() LIMIT 1;")
        row = cursor.fetchone()
        tid = row[0]
        s1 = row[1]
        jj = row[2]
        s2 = row[3]
        dan = row[4]
    c.close()
    conn.close()
    if jj == 0:
        #为加法 
        cvalue = str(s1) + "+" + str(s2) + "=" 
        hd = 1
        ii = random.randint(0,4) #0为提交答案
        if ii == 2:
            cvalue = "□+" + str(s2) + "=" + str(dan) + ",□为"
            plays('□','+',s2,'=',dan)
            dan = s1
        elif ii == 3:
            cvalue = str(s1) + "+□=" + str(dan) + ",□为"
            plays(s1,'+','□','=',dan)
            dan = s2
        elif ii ==4 and s2 > 0:#a+0=a,a-0=a,可以是+-
            cvalue = str(s1) + "□" + str(s2) + "=" + str(dan) + ",□为"
            bffh('fh')
            dan = jj
            hd = 4 #hd4为符号
        else:
            plays(s1,'+',s2,'=','□')
    elif jj ==1:
        cvalue = str(s1) + "-" + str(s2) + "=" 
        hd = 1
        ii = random.randint(0,4) #0为提交答案
        if ii == 2:
            cvalue = "□-" + str(s2) + "=" + str(dan) + ",□为"
            plays('□','-',s2,'=',dan)
            dan = s1
        elif ii == 3:
            cvalue = str(s1) + "-□=" + str(dan) + ",□为"
            plays(s1,'-','□','=',dan)
            dan = s2
        elif ii ==4 and s2 > 0:#a+0=a,a-0=a,可以是+-
            cvalue = str(s1) + "□" + str(s2) + "=" + str(dan) + ",□为"
            bffh('fh')
            dan = jj
            hd = 4 #hd4为符号
        else:
            plays(s1,'-',s2,'=','□')
    elif jj ==2:
        #乘法
        cvalue = str(s1) + "×" + str(s2) + "="
        hd = 1
        ii = random.randint(0,4) #0为提交答案
        if ii == 2 and s2 !=0:#a*0=0 上次程序这个没有限制s2不等于0,bug!
            cvalue = "□×" + str(s2) + "=" + str(dan) + ",□为"
            plays('□','*',s2,'=',dan)
            dan = s1
        elif ii == 3 and s1 != 0:#a*0=0,b*0=0
            cvalue = str(s1) + "×□=" + str(dan) + ",□为"
            plays(s1,'*','□','=',dan)
            dan = s2
        elif ii ==4 and s2 !=1 and s1 != 0:#a*1=a,a/1=a;0*a=0,0/a=0
            cvalue = str(s1) + "□" + str(s2) + "=" + str(dan) + ",□为"
            bffh('fh')
            dan = jj
            hd = 4 #hd4为符号
        else:
            plays(s1,'*',s2,'=','□')
    elif jj ==3:
        cvalue = str(s1) + "÷" + str(s2) + "=" 
        hd = 1
        ii = random.randint(0,4) #0为提交答案
        if ii == 2:
            cvalue = "□÷" + str(s2) + "=" + str(dan) + ",□为"
            plays('□','/',s2,'=',dan)
            dan = s1
        elif ii == 3 and s1 > 0:#0/a=0
            cvalue = str(s1) + "÷□=" + str(dan) + ",□为"
            plays(s1,'/','□','=',dan)
            dan = s2
        elif ii ==4 and s2 !=1 and s1 !=0:#a*1=a,a/1=a;0*a=0,0/a=0
            cvalue = str(s1) + "□" + str(s2) + "=" + str(dan) + ",□为"
            bffh('fh')
            dan = jj
            hd = 4 #hd4为符号
        else:
            plays(s1,'/',s2,'=','□')
    current_date = datetime.datetime.now()
    formatted_date = current_date.strftime("%Y-%m-%d")
    formatted_time = current_date.strftime("%H:%M:%S")
    conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
    c = conn.cursor()
    c.execute("INSERT INTO record (rq,sj,tid,cuo,yt) VALUES (?,?,?,?,?);", (formatted_date,formatted_time,0,0,0))
    conn.commit()
    cursor = c.execute("SELECT cid from record order by cid desc")
    row = cursor.fetchone()
    cid = row[0] 
    c.close()
    conn.close()
    return(jj,dan,hd,cid,tid,cvalue)
#设置题目,并生成记录,用于记录错误,答题用时
def u_record(yt,cid,tid):
    if yt > 0 and cid > 0 and tid >0:
        conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
        c = conn.cursor()                
        c.execute("UPDATE record SET tid = ?, yt = ? WHERE cid = ?;", (tid,yt,cid))
        conn.commit()
        c.execute("UPDATE ys SET cs = cs + 1 WHERE tid = ?;", (tid,))
        conn.commit()        
        c.close()
        conn.close()
#记录答题用时,题目答对次数
def u_cuo(cid,tid):
    if cid > 0 and tid > 0 and globals()['cuo'] <= 1:
        conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
        c = conn.cursor()                
        c.execute("UPDATE record SET cuo = cuo + 1 WHERE cid = ?;", (cid,))
        conn.commit()
        c.execute("UPDATE ys SET cuo = cuo + 1 WHERE tid = ?;", (tid,))
        conn.commit()  
        c.close()
        conn.close()
#记录错误记录
def tj_cs():
    if globals()['hd'] == 4 or globals()['tjcs'] =='':
        conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
        c = conn.cursor()
        query_str = 'select rq,count(cid),sum(cuo),round(avg(yt),0),sum(yt) from record where yt > 0 and yt < 240000 group by rq order by rq desc limit 0,3;'
        cursor = c.execute(query_str)
        rows = cursor.fetchall()
        stext3 = ""
        for row in rows:
            stext3 += "%s:做%d题,错%d次,平均用%d毫秒,学习%d分钟\n" % (row[0],row[1],row[2],row[3],int(row[4]/60000))
        c.close()
        conn.close()
        globals()['tjcs'] = stext3
        stext4 = stext3 + "(已更新)"
    else:
        stext3 = globals()['tjcs']
        stext4 = stext3 + "(待更新)"
    return(stext4)
#显示做题记录
def build(app):
    # 定义组件
    c_box = toga.Box()
    b1_box = toga.Box()
    b2_box = toga.Box()
    b3_box = toga.Box()
    b4_box = toga.Box()
    box = toga.Box()
    c_input = toga.TextInput(readonly=True,style=Pack(font_size=22))
    c_label = toga.Label("请选择难、易开始学习...", style=Pack(text_align=LEFT))
    c_label1 = toga.Label("", style=Pack(text_align=LEFT, font_size=12))
    c_label2 = toga.Label("", style=Pack(text_align=LEFT))
    c_label3 = toga.Label("", style=Pack(text_align=LEFT))
    #界面设置
    def bt1(bb, widget):
        #选择难易,然后出题,答题,再出题
        c_label3.text = tj_cs()
        if bb == '难':
            globals()['nd'] = 2
            c_label.text = '100以内加减乘除。'
        elif bb == '易':
            globals()['nd'] = 1
            c_label.text = '100以内加减。'
        if nd >= 1 and hd == 0:
            globals()['jj'],globals()['dan'],globals()['hd'],globals()['cid'],globals()['tid'],cvalue=settm(nd)
            c_input.value = cvalue
            globals()['cuo'] = 0
            globals()['begintime'] = time()
        if bb in '+-×÷' and hd == 4:
            if bb == '+' and jj == 0:
                c_label1.text = '你答对了'
                ytime = int((time() - begintime) * 1000)
                u_record(ytime,cid,tid)
                c_input.value = c_input.value + bb
                c_label2.text = '复习:' + c_input.value
                globals()['jj'],globals()['dan'],globals()['hd'],globals()['cid'],globals()['tid'],cvalue=settm(nd)
                bb = ''
                globals()['cuo'] = 0
                c_input.value = cvalue
                globals()['begintime'] = time()
            elif bb == '-' and jj == 1:
                c_label1.text = '你答对了'
                ytime = int((time() - begintime) * 1000)
                u_record(ytime,cid,tid)
                c_input.value = c_input.value + bb
                c_label2.text = '复习:' + c_input.value
                globals()['jj'],globals()['dan'],globals()['hd'],globals()['cid'],globals()['tid'],cvalue=settm(nd)
                bb = ''
                globals()['cuo'] = 0
                c_input.value = cvalue
                globals()['begintime'] = time()
            elif bb == '×' and jj == 2:
                c_label1.text = '你答对了'
                ytime = int((time() - begintime) * 1000)
                u_record(ytime,cid,tid)
                c_input.value = c_input.value + bb
                c_label2.text = '复习:' + c_input.value
                globals()['jj'],globals()['dan'],globals()['hd'],globals()['cid'],globals()['tid'],cvalue=settm(nd)
                bb = ''
                globals()['cuo'] = 0
                c_input.value = cvalue
                globals()['begintime'] = time()
            elif bb == '÷' and jj == 3:
                c_label1.text = '你答对了'
                ytime = int((time() - begintime) * 1000)
                u_record(ytime,cid,tid)
                c_input.value = c_input.value + bb
                c_label2.text = '复习:' + c_input.value
                globals()['jj'],globals()['dan'],globals()['hd'],globals()['cid'],globals()['tid'],cvalue=settm(nd)
                bb = ''
                globals()['cuo'] = 0
                c_input.value = cvalue
                globals()['begintime'] = time()
            else:
                c_label1.text = '答错了,重新回答'
                globals()['cuo'] += 1
                u_cuo(cid,tid)
                globals()['hd'] = 4
        if is_number(bb) and hd == 3:
            dan3 = 0
            if str(dan3) == bb:
                c_label1.text = '你答对了'
                ytime = int((time() - begintime) * 1000)
                u_record(ytime,cid,tid)
                c_input.value = c_input.value + bb
                c_label2.text = '复习:' + c_input.value
                globals()['jj'],globals()['dan'],globals()['hd'],globals()['cid'],globals()['tid'],cvalue=settm(nd)
                bb = ''
                globals()['cuo'] = 0
                c_input.value = cvalue
                globals()['begintime'] = time()
            else:
                c_label1.text = '答错了,你只答对了2个数字,请输入下一个数字'
                globals()['cuo'] += 1
                u_cuo(cid,tid)
                globals()['hd'] = 3

        if is_number(bb) and hd == 2:
            if dan >= 10 and dan < 100:
                dan2 = int(str(dan)[-1:])
                if dan2 == int(bb):
                    c_label1.text = '你答对了'
                    ytime = int((time() - begintime) * 1000)
                    u_record(ytime,cid,tid)
                    c_input.value = c_input.value + bb
                    globals()['hd'] = 0 #答对重新出题
                    c_label2.text = '复习:' + c_input.value
                    globals()['jj'],globals()['dan'],globals()['hd'],globals()['cid'],globals()['tid'],cvalue=settm(nd)
                    bb = ''
                    globals()['cuo'] = 0
                    c_input.value = cvalue
                    globals()['begintime'] = time()
                else:
                    c_label1.text = '答错了,你只答对了1个数字,请输入下一个数字'
                    globals()['cuo'] += 1
                    u_cuo(cid,tid)
                    #globals()['hd'] = 2
            elif dan ==100:
                dan2 = 0
                if str(dan2) == bb:
                    c_input.value = c_input.value + bb
                    c_label1.text = '你又答对了1个数字,请输入下一个数字'
                    globals()['hd'] = 3
                else:
                    c_label1.text = '你答错了,继续答题'
                    globals()['cuo'] += 1
                    u_cuo(cid,tid)
                    
        if is_number(bb) and hd == 1:
            if dan < 10:
                if dan == int(bb):
                    c_label1.text = '你答对了'
                    ytime = int((time() - begintime) * 1000)
                    u_record(ytime,cid,tid)
                    c_input.value = c_input.value + bb
                    c_label2.text = '复习:' + c_input.value
                    globals()['jj'],globals()['dan'],globals()['hd'],globals()['cid'],globals()['tid'],cvalue=settm(nd)
                    bb = ''
                    globals()['cuo'] = 0
                    c_input.value = cvalue
                    globals()['begintime'] = time()
                else:
                    c_label1.text = '你答错了,继续答题'
                    globals()['cuo'] += 1
                    u_cuo(cid,tid)
            elif dan >= 10 and dan < 100:
                dan1 = int(str(dan)[:1])
                #print(dan1)
                if dan1 == int(bb):
                    c_label1.text = '你答对了1个数字,请输入下一个数字'
                    c_input.value = c_input.value + bb
                    globals()['hd'] = 2
                else:
                    globals()['cuo'] += 1
                    u_cuo(cid,tid)
                #    c_label1.text = '你答错了,继续答题'                
            elif dan == 100:
                dan1 = 1
                if dan1 == int(bb):
                    c_input.value = c_input.value + bb
                    c_label1.text = '你答对了1个数字,请输入下一个数字'
                    globals()['hd'] = 2
                else:
                    globals()['cuo'] += 1
                    u_cuo(cid,tid)

    aaa = '7 8 9 + 4 5 6 - 1 2 3 × 0 易 难 ÷'.split()
    #print(aaa) 用字符串变为数组,这样代码比较简
    for i in range(1,17):
        names['button_' + str(i)] = toga.Button(aaa[i-1], on_press=partial(bt1, aaa[i-1]),style=Pack(font_size=25))
    #初始界面
    # 设置组件样式和布局
    c_box.add(c_input)
    box.add(c_box)
    for i in range(1,5):
        b1_box.add(names['button_' + str(i)])
    for i in range(5,9):
        b2_box.add(names['button_' + str(i)])
    for i in range(9,13):
        b3_box.add(names['button_' + str(i)])
    for i in range(13,17):
        b4_box.add(names['button_' + str(i)])
    box.add(b1_box)
    box.add(b2_box)
    box.add(b3_box)
    box.add(b4_box)
    box.add(c_label)
    box.add(c_label1)
    box.add(c_label2)
    box.add(c_label3)
    # 设置 outer box 和 inner box 的样式
    box.style.update(direction=COLUMN, padding=5)
    b1_box.style.update(direction=ROW, padding=1)
    b2_box.style.update(direction=ROW, padding=1)
    b3_box.style.update(direction=ROW, padding=1)
    b4_box.style.update(direction=ROW, padding=1)
    c_box.style.update(direction=ROW, padding=5)

    # 设置单个组件的样式
    c_input.style.update(width=345, flex=1)

    # button.style.update(padding=15)
    c_label.style.update(width=345, padding_left=4)
    c_label1.style.update(width=345, padding_left=4)
    c_label2.style.update(width=345, padding_left=4)
    c_label3.style.update(width=345, padding_left=4)
    for i in range(1,17):
        names['button_' + str(i)].style.update(width=85, height=85, padding=1)

    return box
def plays(t1,tfh1,t2,tfh2,tdan):
    thread = threading.Thread(target=bfsz, args=(t1,tfh1,t2,tfh2,tdan,))
    thread.daemon = True
    thread.start()
def pfh(fh):
    thread = threading.Thread(target=bffh, args=(fh,))
    thread.daemon = True
    thread.start()
def pnum(num):
    thread = threading.Thread(target=bfnum, args=(num,))
    thread.daemon = True
    thread.start()
def main():
    return toga.App("千纬数学", "org.qwsx", startup=build)

if __name__ == "__main__":
    main().main_loop()

说明:这里面采用多线程播放声音,中间用sleep来处理,让声音加快播放,即播放间距加密,这是没有办法的办法。

  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值