前言:
由于Python考试临近,加上平时要学习英语,突发奇想制作一款和手机上app那样的记单词的简单软件,顺便复习一下Python。
步骤:
- 编辑一个保存单词的txt文档
- 读取txt文件,制作成字典
- 使用tkinter制作界面
- 使用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文件中,这样才能正常使用。
这是可正常使用的文件夹。
这样发送给别人也能正常使用。