1, 手机和安卓平板的Qpython 下运行有声音
2, mac和win也应该没有问题
3, 单词库自己录入后放到同一文件夹下就行,怎么命名看程序代码
4,单词库的格式
===========
序号 <换行>
英语 <换行>
中文 <换行>
。。。。。。循环,每三行一个轮回
import os,sys, random,time
try:
import pyttsx3
except:
pass
try:
import androidhelper
except:
pass
class Edu:
def __init__(self):
try:
self.init_sound()
except:
print("......语音启动失败......")
try:
self.droid=androidhelper.Android()
except:
print("......init Androidhelper fail......")
os.chdir(sys.path[0])
print(f"path={sys.path[0]}")
def init_parameter(self):
#变量初始化
self.chooseNb=0
self.sig=0
self.ttlqty=0
self.goodqty=0
self.badqty=0
self.filename="joinin0301.txt"
self.words=[]
self.cwords=[]
self.wrongwords=[]
self.wordqty=0
self.engine=0
self.starttime=0
self.endtime=0
self.ttlwords=0
self.errorprompt=['哎呀呀呀','出错了','好好练习','傻了吧','集中精力','感觉你有点蠢']
def ClearScreen(self):
#针对不同的OS清空屏幕
if os.name=="nt":
os.system("cls")
else:
os.system("clear")
def run(self):
#完整的程序运行流程
self.init_parameter()
self.UI_choose()
self.ReadTextFile()
self.UI_game()
def init_sound(self):
#发声系统初始化
self.engine=pyttsx3.init()
# 设置发音速率,默认值为200
rate = self.engine.getProperty('rate')
self.engine.setProperty('rate', rate)
# 设置发音大小,范围为0.0-1.0
volume = self.engine.getProperty('volume')
self.engine.setProperty('volume', 0.6)
# 设置默认的声音:voices[0].id代表男生,voices[1].id代表女生
voices = self.engine.getProperty('voices')
self.engine.setProperty('voice', voices[1].id)
def UI_choose(self):
#选课本 界面
self.ClearScreen()
print("---*** 欢迎进入一凡学习系统 ***---")
print("1. 26个字母")
print("2. 计算机常用单词")
print("3. 3年级英语单词")
print("4. 自定义的英语单词")
self.choiseNb=input("请选择你所学习项目的编号......(输入‘qqq’则退出)")
if self.choiseNb=="1":
self.filename="a2z.txt"
if self.choiseNb=="2":
self.filename="coding.txt"
if self.choiseNb=="3":
self.filename="joinin0301.txt"
if self.choiseNb=="4":
self.filename="lib.txt"
if self.choiseNb=="qqq":
_=input("bye-bye, see u next time!")
exit(0)
def ReadTextFile(self):
#读取选定的课本
linei=0
with open(self.filename, encoding= 'UTF-8') as file_to_read:
while True:
lines = file_to_read.readline() # 整行读取数据
if not lines:
break
else:
lines=lines.strip()
if linei%3==1:
self.words.append(lines)
lines=file_to_read.readline()
lines=lines.strip()
self.cwords.append(lines)
linei =1
linei =1
self.wordsqty=len(self.words)
def show_time(self,whichone):
self.endtime=time.time()
actualtime=self.endtime-self.starttime
actualtime=int(actualtime*100)/100
if actualtime!=0:
actualspeed=(self.ttlwords/4)/(actualtime/60)
else:
actualspeed=0
actualspeed=int(actualspeed*100)/100
print(f"题号:{whichone} 时长:{actualtime} 字符数:{self.ttlwords} 速度:{actualspeed}")
def UI_game(self):
#游戏 界面
print(" ------------------------ ")
print("X:0学习/1打字/2提示默写/3无提示默写")
print("Y:0顺序/1随机")
print("Z:起始题号")
print(" ------------------------ ")
tmpstr=input("---请输入学习模式 [xyz格式]---")
if len(tmpstr)==0: self.UI_game()
if tmpstr=="qqq": self.run()
edutype=tmpstr[0:1]
seqtype=tmpstr[1:2]
if len(tmpstr)>2:
startno=int(tmpstr[2:])
else:
startno=0
print(f"学习类型: {edutype} && 顺序类型={seqtype} && 起始题号={startno}")
_=input("")
self.starttime=time.time()
while True:
self.ClearScreen()
#按顺序 或 随机读取
if seqtype=="0":
whichone=self.ttlqty startno
else:
whichone=random.randint(0,self.wordsqty-1)
request_word=self.words[whichone]
self.show_time(whichone)
if edutype=="0": #Touring MODE
print(f"共计:{self.ttlqty}题 Curent No:{self.badqty}")
tmpstr=f"==>\033[0;31;40m\t {request_word} \033[0m [ {self.cwords[whichone]}]"
print(tmpstr)
try:
self.droid.ttsSpeak(request_word self.cwords[whichone])
except:
pass
try:
self.engine.say(request_word self.cwords[whichone])
self.engine.runAndWait()
except:
pass
user_input=input()
if user_input == "qqq":
self.show_time(whichone)
_=input(f"共计:{self.ttlqty}题")
self.run()
else:
self.ttlqty =1
if self.ttlqty==self.wordsqty: self.run()
if edutype=="1": #typing MODE
print(f"共计:{self.ttlqty}题 正确:{self.goodqty} 错误:{self.badqty}")
tmpstr=f"==>\033[0;31;40m\t {request_word} \033[0m [ {self.cwords[whichone]}]"
print(tmpstr)
print("==> ",end="")
try:
self.droid.ttsSpeak(request_word)
except:
pass
try:
self.engine.say(request_word)
self.engine.runAndWait()
except:
pass
user_input=input()
if len(user_input)==0:
tmpstr=f"==>\033[0;31;40m\t {request_word} \033[0m [ {self.cwords[whichone]}]"
print(tmpstr)
_=input("没有输入")
self.wrongwords.append(request_word " [" self.cwords[whichone] "]")
self.ttlqty =1
self.badqty =1
else:
if user_input=="qqq":
print("---错误清单---")
for i in self.wrongwords:
print(i)
self.show_time(whichone)
_=input(f"共计:{self.ttlqty}题 正确:{self.goodqty} 错误:{self.badqty}")
self.run()
self.ttlqty =1
if user_input.strip()==request_word.strip():
self.ttlwords =len(request_word) 1
self.goodqty =1
else:
self.wrongwords.append(request_word " [" self.cwords[whichone] "]" " 错误原因:[ " user_input " ]")
self.ttlwords =len(user_input) 1
self.badqty =1
tmprnd=random.randint(0,5)
try:
self.droid.ttsSpeak(self.errorprompt[tmprnd])
except:
pass
tmpstr=f"==>\033[0;31;40m\t {request_word} \033[0m [ {self.cwords[whichone]}]"
print(tmpstr)
_=input(" - 哎呀, 出错了! - ")
if edutype=="2" or edutype=="3": #recite MODE
print(f"共计:{self.ttlqty}题 正确:{self.goodqty} 错误:{self.badqty}")
#tmpstr=f"==>\033[0;31;40m\t {request_word} \033[0m [ {self.cwords[whichone]}]"
if edutype=="2":
tmpstr=f"==>\033[0;31;40m\t [{self.cwords[whichone]}] \033[0m "
else:
tmpstr=""
print(tmpstr)
print("==> ",end="")
try:
self.droid.ttsSpeak(request_word)
except:
pass
try:
self.engine.say(request_word)
self.engine.runAndWait()
except:
pass
user_input=input()
if len(user_input)==0:
tmpstr=f"==>\033[0;31;40m\t {request_word} \033[0m [ {self.cwords[whichone]}]"
print(tmpstr)
_=input("没有输入")
self.wrongwords.append(request_word " [" self.cwords[whichone] "]")
self.ttlqty =1
self.badqty =1
else:
if user_input=="qqq":
print("---错误清单---")
for i in self.wrongwords:
print(i)
self.show_time(whichone)
_=input(f"共计:{self.ttlqty}题 正确:{self.goodqty} 错误:{self.badqty}")
self.run()
self.ttlqty =1
if user_input.strip()==request_word.strip():
self.ttlwords =len(request_word) 1
self.goodqty =1
else:
self.wrongwords.append(request_word " [" self.cwords[whichone] "]" " 错误原因:[ " user_input " ]")
self.ttlwords =len(user_input) 1
self.badqty =1
tmprnd=random.randint(0,5)
try:
self.droid.ttsSpeak(self.errorprompt[tmprnd])
except:
pass
tmpstr=f"==>\033[0;31;40m\t {request_word} \033[0m [ {self.cwords[whichone]}]"
print(tmpstr)
_=input(" - 哎呀, 出错了! - ")
if __name__=="__main__":
game=Edu()
game.run()