之前写了:制作一个给小学生练习100以内四则运算的app-CSDN博客
今天,结合发现beeware生成android程序,需要把数据库文件放在data/data/包名/的文件夹中,才能保存数据,而不用去让app获取手机文件读写权限,能更方便大家使用。加上将答数学运算题的答题速度计算积分,以增加小学生答题兴趣,并让家长能知道孩子的学习情况,引入积分机制,更新了原有app,并发布正式版。
源码如下:
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")
#测试参数1为手机,0为电脑,2为boox平板
sjcs = 1
if sjcs == 1:
sj_file = '/data/data/com.qwsx.qwsx/qwsx.db'
if Path(sj_file).exists():
db_filepath = sj_file
else:
shutil.copy(db_filepath, sj_file)
db_filepath = sj_file
elif sjcs == 2:
db_filepath = '/storage/emulated/0/Pictures/qwsx.db'
conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
c = conn.cursor()
current_date = datetime.datetime.now()
formatted_date = current_date.strftime("%Y-%m-%d")
cursor = c.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='jfen'")
row = cursor.fetchone()
have_jfena = row[0]
if have_jfena == 0:
c.execute('''CREATE TABLE jfen (
jid INTEGER PRIMARY KEY AUTOINCREMENT,
rq TEXT,
jf INTEGER);
''')
conn.commit()
cursor = c.execute("SELECT count(*) FROM jfen WHERE rq='"+ formatted_date +"'")
row = cursor.fetchone()
if row[0] == 0:
query_sql = "INSERT INTO jfen (rq,jf) VALUES (?,?);"
c.execute(query_sql, (formatted_date,0,))
conn.commit()
c.close()
conn.close()
#建积分表,并插入一行今日积分
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 u_jfen(jf):
if jf !=0:
conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
c = conn.cursor()
current_date = datetime.datetime.now()
formatted_date = current_date.strftime("%Y-%m-%d")
cursor = c.execute("SELECT count(*) FROM jfen WHERE rq='"+ formatted_date +"'")
row = cursor.fetchone()
if row[0] ==0:
query_sql = "INSERT INTO jfen (rq,jf) VALUES (?,?);"
c.execute(query_sql, (formatted_date,0,))
conn.commit()
query_sql = "update jfen set jf = jf + ? where rq= ?"
c.execute(query_sql, (jf,formatted_date,))
conn.commit()
else:
query_sql = "update jfen set jf = jf + ? where rq= ?"
c.execute(query_sql, (jf,formatted_date,))
conn.commit()
c.close()
conn.close()
#更新积分,增加积分,中间再次判断今日的数据行是否存在是为了跨日做题不出错
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)
#上面用sleep调节播放数字和运算符号的速度,可以再修改
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 jf_ytime(ytime):
if ytime < 1000:
jfen = 20
elif ytime >=1000 and ytime <2000:
jfen = 17
elif ytime >=2000 and ytime <3000:
jfen = 14
elif ytime >=3000 and ytime <4000:
jfen = 10
elif ytime >=4000 and ytime <5000:
jfen = 7
elif ytime >=5000 and ytime <6000:
jfen = 4
elif ytime >=6000 and ytime <7000:
jfen = 3
elif ytime >=7000 and ytime <8000:
jfen = 2
elif ytime >=8000 and ytime <20000:
jfen = 1
else:
jfen = 0
return(jfen)
#用时转化为积分的表,大于20秒不给积分,反应越快积分越高
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()
cursor = c.execute("SELECT sum(jf) from jfen")
row = cursor.fetchone()
zjf = row[0]
current_date = datetime.datetime.now()
formatted_date = current_date.strftime("%Y-%m-%d")
cursor = c.execute("SELECT jf from jfen where rq = '" + formatted_date + "' limit 0,1")
row = cursor.fetchone()
jtjf = row[0]
stext3 = "总积分:%d,今日积分:%d \n" % (zjf,jtjf)
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()
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:
ytime = int((time() - begintime) * 1000)
if bb == '+' and jj == 0:
jf = jf_ytime(ytime)
c_label1.text = '你答对了' + ',本题取得' + str(jf) + '积分'
u_record(ytime,cid,tid)
u_jfen(jf)
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:
jf = jf_ytime(ytime)
c_label1.text = '你答对了' + ',本题取得' + str(jf) + '积分'
u_record(ytime,cid,tid)
u_jfen(jf)
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:
jf = jf_ytime(ytime)
c_label1.text = '你答对了' + ',本题取得' + str(jf) + '积分'
u_record(ytime,cid,tid)
u_jfen(jf)
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:
jf = jf_ytime(ytime)
c_label1.text = '你答对了' + ',本题取得' + str(jf) + '积分'
u_record(ytime,cid,tid)
u_jfen(jf)
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 = '答错了,重新回答,本次扣15分'
globals()['cuo'] += 1
u_cuo(cid,tid)
jf = -15
u_jfen(jf)
globals()['hd'] = 4
if is_number(bb) and hd == 3:
ytime = int((time() - begintime) * 1000)
dan3 = 0
if str(dan3) == bb:
jf = jf_ytime(ytime)
c_label1.text = '你答对了' + ',本题取得' + str(jf) + '积分'
u_record(ytime,cid,tid)
u_jfen(jf)
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 = '答错了,本次扣10分,答对了2个数字,请输入下一个'
globals()['cuo'] += 1
u_cuo(cid,tid)
jf = -10
u_jfen(jf)
globals()['hd'] = 3
if is_number(bb) and hd == 2:
ytime = int((time() - begintime) * 1000)
if dan >= 10 and dan < 100:
dan2 = int(str(dan)[-1:])
if dan2 == int(bb):
jf = jf_ytime(ytime)
c_label1.text = '你答对了' + ',本题取得' + str(jf) + '积分'
u_record(ytime,cid,tid)
u_jfen(jf)
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 = '答错了,本次扣10分,你只答对了1个数字,请输入下一个数字'
globals()['cuo'] += 1
u_cuo(cid,tid)
jf = -10
u_jfen(jf)
#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 = '你答错了,继续答题,本次扣10分'
globals()['cuo'] += 1
u_cuo(cid,tid)
jf = -10
u_jfen(jf)
if is_number(bb) and hd == 1:
if dan < 10:
ytime = int((time() - begintime) * 1000)
if dan == int(bb):
jf = jf_ytime(ytime)
c_label1.text = '你答对了' + ',本题取得' + str(jf) + '积分'
u_record(ytime,cid,tid)
u_jfen(jf)
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 = '你答错了,继续答题,本次扣15分'
globals()['cuo'] += 1
u_cuo(cid,tid)
jf = -15
u_jfen(jf)
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)
jf = -8
u_jfen(jf)
# 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)
jf = -8
u_jfen(jf)
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()
app下载:链接:https://pan.baidu.com/s/16Ci3po_eVcBbqiGHYjkB5Q?pwd=4mde
提取码:4mde