一、准备好成语,为了让小孩能够学习一些不熟悉成语,我特意采集成语的拼音、解释、示例、出处等。采集了2天,这个过程不光彩就不写采集的代码了,成语一共有30000个。然后再采集小学、中学及常用的成语,与30000个成语碰撞,列成不同的表,再表中增加一列记录练习的次数。
二、开始编程,这个界面与我之前开发的差不多,就为了快点上手。成语拆成2个2字的词语,要连续点击连成4字成语就会消去,共12个成语,消去10个就过另外一个。
"""
qwcy 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
import textwrap
#导入必要的库
resources_folder = Path(__file__).joinpath("../resources/").resolve()
db_filepath = resources_folder.joinpath("cy.db")
#测试参数1为手机,0为电脑,2为boox平板
sjcs = 1
if sjcs == 1:
sj_file = '/data/data/com.qwcy.qwcy/cy.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/cy.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()
nd = 0
cixu = 0 #答题次数12 34 56组成成语
cyq = '' #成语前面2个字
cyh = '' #成语后面2个字
#建积分表,并插入一行今日积分
names = globals()
# str0 = '半途而废'
# print(str0[2:])
def set_all():
conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
c = conn.cursor()
if globals()['nd'] == 0:
if random.randint(0, 1) == 1:
cursor = c.execute("SELECT id, cy, fy, sy, cc, sl from cy where id in (select dyid from xxlx) and length(cy) = 4 order by random() limit 0,12;")
else:
cursor = c.execute("SELECT aa.dyid,bb.cy,bb.fy,bb.sy,bb.cc,bb.sl FROM xxlx aa JOIN cy bb ON aa.dyid = bb.id where length(bb.cy) = 4 order by aa.cs asc limit 0,12;")
elif globals()['nd'] == 1:
if random.randint(0, 1) == 1:
cursor = c.execute("SELECT id, cy, fy, sy, cc, sl from cy where id in (select dyid from zxlx) and length(cy) = 4 order by random() limit 0,12;")
else:
cursor = c.execute("SELECT aa.dyid,bb.cy,bb.fy,bb.sy,bb.cc,bb.sl FROM zxlx aa JOIN cy bb ON aa.dyid = bb.id where length(bb.cy) = 4 order by aa.cs asc limit 0,12;")
elif globals()['nd'] == 2:
if random.randint(0, 1) == 1:
cursor = c.execute("SELECT id, cy, fy, sy, cc, sl from cy where id in (select dyid from cylx) and length(cy) = 4 order by random() limit 0,12;")
else:
cursor = c.execute("SELECT aa.dyid,bb.cy,bb.fy,bb.sy,bb.cc,bb.sl FROM cylx aa JOIN cy bb ON aa.dyid = bb.id where length(bb.cy) = 4 order by aa.cs asc limit 0,12;")
else:
cursor = c.execute("SELECT id, cy, fy, sy, cc, sl from cy where length(cy) = 4 order by random() limit 0,12;")
rows = cursor.fetchall()
dyid = []
cy = []
fy = []
sy = []
cc = []
sl = []
for row in rows:
dyid.append(row[0])
dyid.append(row[0])
cy.append(row[1][:2])
cy.append(row[1][2:])
fy.append(str(row[2]))
fy.append(str(row[2]))
sy.append(str(row[3]))
sy.append(str(row[3]))
cc.append(str(row[4]))
cc.append(str(row[4]))
sl.append(str(row[5]))
sl.append(str(row[5]))
ii = list(range(1,25))
random.shuffle(ii)
c.close()
conn.close()
return dyid,ii,cy,fy,sy,cc,sl,
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_cs(dyid):
conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
c = conn.cursor()
if globals()['nd'] == 0:
query_sql = "update xxlx set cs = cs + 1 where dyid= ?"
elif globals()['nd'] == 1:
query_sql = "update zxlx set cs = cs + 1 where dyid= ?"
elif globals()['nd'] == 2:
query_sql = "update cylx set cs = cs + 1 where dyid= ?"
c.execute(query_sql, (dyid,))
conn.commit()
c.close()
conn.close()
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 wrapstr(tt):
if sjcs == 0:
wrapped_text = textwrap.fill(tt.replace('\n',''), 24)
else:
wrapped_text = textwrap.fill(tt.replace('\n',''), 25)
# tts = ''
# for line in wrapped_text:
# tts = tts + line +'\n'
return wrapped_text
def build(app):
# 定义组件
c_box = toga.Box()
b1_box = toga.Box()
b2_box = toga.Box()
b3_box = toga.Box()
b4_box = toga.Box()
b5_box = toga.Box()
b6_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,padding=1))
c_label2 = toga.Label("", style=Pack(text_align=LEFT, font_size=10,padding=1))
c_label3 = toga.Label("", style=Pack(text_align=LEFT, font_size=10,padding=1))
#界面设置
def bt1(bb, widget):
if '.' in bb:
if bb == '小学.':
globals()['nd'] = 0
elif bb == '中学.':
globals()['nd'] = 1
elif bb == '常用.':
globals()['nd'] = 2
else:
globals()['nd'] = 3
globals()['cixu'] = 0
globals()['cyq'] = ''
globals()['cyh'] = ''
globals()['dyid'],globals()['ii'],globals()['cy'],globals()['fy'],globals()['sy'],globals()['cc'],globals()['sl']=set_all()
for i in range(1,25):
setattr(names['button_' + str(i)],"text",cy[ii[i-1]-1])
setattr(names['button_' + str(i)],"on_press",partial(bt1, cy[ii[i-1]-1]))
setattr(names['button_' + str(i)],"enabled",True)
else:
if cy.index(bb) % 2 == 0:
c_input.value = bb + '□□'
globals()['cyq'] = bb
else:
c_input.value = '□□' + bb
globals()['cyh'] = bb
c_label1.text = '拼音:' + fy[cy.index(bb)]
c_label2.text = wrapstr('解释:' + sy[cy.index(bb)])
c_label3.text = wrapstr('例子:' + sl[cy.index(bb)])
# print(cy.index(bb),ii.index(cy.index(bb)+1))
allcy = []
i = 0
while i < 24:
if i%2 ==0:
allcy.append(globals()['cy'][i]+globals()['cy'][i+1])
i = i + 1
# print(allcy)
if (globals()['cyq'] + globals()['cyh']) in allcy:
# print('你答对了!')
c_input.value = globals()['cyq'] + globals()['cyh'] + ' 你答对了!'
if globals()['nd'] != 3:
u_cs(dyid[cy.index(bb)])
u_jfen(globals()['nd']+2)
setattr(names['button_' + str(ii.index(cy.index(globals()['cyq'])+1)+1)],"text",'')
setattr(names['button_' + str(ii.index(cy.index(globals()['cyq'])+1)+1)],"enabled",False)
setattr(names['button_' + str(ii.index(cy.index(globals()['cyh'])+1)+1)],"text",'')
setattr(names['button_' + str(ii.index(cy.index(globals()['cyh'])+1)+1)],"enabled",False)
globals()['cixu'] = globals()['cixu'] + 1
if globals()['cixu'] >= 10:
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 sum(jf) from jfen")
row = cursor.fetchone()
zjf = row[0]
cursor = c.execute("SELECT jf from jfen where rq = '" + formatted_date + "' limit 0,1")
row = cursor.fetchone()
jtjf = row[0]
c.close()
conn.close()
c_label.text = '总积分:' + str(zjf) + ',今日获得:' + str(jtjf)
globals()['cixu'] = 0
globals()['cyq'] = ''
globals()['cyh'] = ''
globals()['dyid'],globals()['ii'],globals()['cy'],globals()['fy'],globals()['sy'],globals()['cc'],globals()['sl']=set_all()
for i in range(1,25):
setattr(names['button_' + str(i)],"text",cy[ii[i-1]-1])
setattr(names['button_' + str(i)],"on_press",partial(bt1, cy[ii[i-1]-1]))
setattr(names['button_' + str(i)],"enabled",True)
aaa = '小学. 中学. 常用. 全部. 小学. 中学. 常用. 全部. 小学. 中学. 常用. 全部. 小学. 中学. 常用. 全部. 小学. 中学. 常用. 全部. 小学. 中学. 常用. 全部.'.split()
# globals()['dyid'],globals()['ii'],globals()['cy'],globals()['fy'],globals()['sy'],globals()['cc'],globals()['sl']=set_all()
#print(aaa) 用字符串变为数组,这样代码比较简
for i in range(1,25):
# names['button_' + str(i)] = toga.Button(cy[ii[i-1]-1], on_press=partial(bt1, cy[ii[i-1]-1]),style=Pack(font_size=18))
names['button_' + str(i)] = toga.Button(aaa[i-1], on_press=partial(bt1, aaa[i-1]),style=Pack(font_size=18))
#初始界面
# 设置组件样式和布局
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)])
for i in range(17,21):
b5_box.add(names['button_' + str(i)])
for i in range(21,25):
b6_box.add(names['button_' + str(i)])
box.add(b1_box)
box.add(b2_box)
box.add(b3_box)
box.add(b4_box)
box.add(b5_box)
box.add(b6_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)
b5_box.style.update(direction=ROW, padding=1)
b6_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,25):
names['button_' + str(i)].style.update(width=85, height=50, padding=1)
return box
def main():
return toga.App("千纬成语消消乐", "org.qwcy", startup=build)
if __name__ == "__main__":
main().main_loop()