使用Python制作学习英语软件

前言:
由于Python考试临近,加上平时要学习英语,突发奇想制作一款和手机上app那样的记单词的简单软件,顺便复习一下Python。

步骤:

  1. 编辑一个保存单词的txt文档
  2. 读取txt文件,制作成字典
  3. 使用tkinter制作界面
  4. 使用pyinstaller打包成exe程序文件

文件目录
文件链接

-StudyEnglish
 --Word
   ---Unit20.txt
   ---Unit19.txt
 --config.py
 --GetWord.py
 --StudyEnglish.py

1.编辑一个保存单词的txt文档
在这里插入图片描述
格式如下,单词和词义用’,'分开,不同词义用;分开。

2.读取txt文件,制作成字典
使用os库读取txt文件
代码如下

import os
import Config as cf

def get_word():
    word_dic = {}
    word_list = []
    for root,dirs,files in os.walk(cf.Word_path):
        #访问路径下所有文件,然后遍历
        for file in files:
            #open用read模式打开文件
            with open(root+'/'+file,'r',encoding='UTF-8') as w:
                #读txt文件每行并遍历
                words=w.readlines()
                for word in words:
                    #使用','进行分割
                    w_m=word.split(',')
                    w=w_m[0]
                    m=w_m[1][:-1]
                    #保存进字典,列表
                    word_dic[w]=m
                    word_dic[m]=w
                    word_list.append(m)
                    word_list.append(w)
        return word_dic,word_list

3.制作tkinter制作界面
界面如下,因为对tkinter掌握不多,所以界面比较简陋hhh,后期可能会完善。
在这里插入图片描述
代码如下:

from tkinter import *
from GetWord import get_word
import random
import Config as cf
import tkinter.messagebox as box

##使用random随机打乱数据
word_dir, word_list = get_word()
num = random.randint(0,len(word_list))
random.seed(num)
data=random.randint(0,len(word_list))

root=Tk()
root['width']=500
root['height']=500
root.resizable(False,False)

##创建文本标签
labelName1=Label(root,text='开卷(卷第三声)!!!',font=('宋体',38))
labelName1.place(x=0,y=0,width=500,height=100)
labelName2=Label(root,text='题目',font=('宋体',38))
labelName2.place(x=0,y=100,width=100,height=100)
labelName3=Label(root,text='答案',font=('宋体',38))
labelName3.place(x=0,y=200,width=100,height=100)

##创建题目栏,设置为只读
titlevalue1=StringVar(root,value='')
entrytitle1=Entry(root,textvariable=titlevalue1,state='readonly',font=('宋体',38))
entrytitle1.place(x=200,y=100,width=400,height=100)
titlevalue1.set(word_list[data])

##创建答题栏,设置为只读
titlevalue2=StringVar(root,value='')
entrytitle2=Entry(root,textvariable=titlevalue2,font=('宋体',38))
entrytitle2.place(x=200,y=200,width=400,height=100)

##创建评价指南文本标签
losslabel=Label(root,text='错误率',font=('宋体',30))
losslabel.place(x=0,y=300,width=125,height=100)

acclabel=Label(root,text='准确率',font=('宋体',30))
acclabel.place(x=250,y=300,width=125,height=100)

##创建评价指南,设置为只读
lossvalue=StringVar(root,value='')
entryloss=Entry(root,textvariable=lossvalue,state='readonly',font=('宋体',38))
entryloss.place(x=125,y=300,width=125,height=100)
lossvalue.set(cf.loss)

accvalue=StringVar(root,value='')
entryacc=Entry(root,textvariable=accvalue,state='readonly',font=('宋体',38))
entryacc.place(x=375,y=300,width=125,height=100)
accvalue.set(cf.acc)

##答题按键触发函数
def judge():
    global data
    result = entrytitle2.get()
    cf.num += 1
    if result in word_dir.get(word_list[data]):
        cf.accnum += 1
    else:
        cf.lossnum += 1
        root1 = Tk()
        root1['width'] = 300
        root1['height'] = 100
        root1.resizable(False, False)
        resultName =Label(root1, text='%s:%s'%(word_list[data],word_dir.get(word_list[data])), font=('宋体', 20))
        resultName.place(x=0, y=0, width=300, height=100)

    titlevalue2.set('')
    cf.loss = cf.lossnum / cf.num
    cf.acc = cf.accnum / cf.num

    data = random.randint(0, len(word_list))
    titlevalue1.set(word_list[data])
    lossvalue.set(cf.loss)
    accvalue.set(cf.acc)


##保存单词触发函数
def set_data():
    root2=Tk()
    root2['width']=300
    root2['height']=300
    newword=StringVar(root2,value='')
    wordentry=Entry(root2,textvariable=newword, font=(15))
    wordentry.place(x=0,y=0,width=300,height=100)
    
    #添加单词函数
    def add_word():
        word = newword.get()
        if ',' not in word:
            box.showerror(title='',message='格式错误')
        elif ',' in word:
            word +='\n'
            with open('./Word/Unit19.txt','a',encoding='UTF-8') as w:
                w.writelines(word)
                w.close()
            newword.set('')
        else:
            pass

    laebl = Label(root2,text='格式:ABC,英文逗号', font=('宋体', 25))
    laebl.place(x=0,y=200,width=300,height=100)

    okkey = Button(root2, text='添加', command=add_word, font=('宋体', 38))
    okkey.place(x=0, y=100, width=300, height=100)


##答题按键
okkey=Button(root,text='确认',command=judge,font=('宋体',38))
okkey.place(x=0,y=400,width=250,height=100)
##添加单词按键
set=Button(root,text='添加单词',command=set_data,font=('宋体',38))
set.place(x=250,y=400,width=250,height=100)

root.mainloop()

4.使用pyinstaller打包成exe程序文件

安装pyinstaller

pip install Pyinstaller

装不了可以换源。
在StudyEnglish打开终端
在这里插入图片描述
终端输入

pyinstaller -F -w StudyEnglish.py

这是打包好的目录
在这里插入图片描述
在dist文件夹中有StudyEnglish.exe文件,但是,由于我在StudyEnglish.py文件中使用了其他文件,如果直接打开exe文件会报错,所以要把config.py和GetWord.py还有Word移动到dist文件中,这样才能正常使用。
这是可正常使用的文件夹。
在这里插入图片描述
这样发送给别人也能正常使用。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月明Mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值