总览

  • 前言
  • 主要函数
  • 定义Next函数,背下一个单词
  • 定义delete函数,删除过于简单的单词
  • 定义dif函数,把较难的词汇加入“难词.txt”
  • 运行
  • 开始
  • 主界面
  • 下一个
  • 背完
  • 复习
  • 核对
  • 结束
  • 查看结果
  • 代码获取


前言

主要依赖于python的tkinter实现英语背单词的程序,通过选择六级/四级/考研词汇,读取文件的单词,随机选择单词,背诵并复习。

主要函数

##选择单词文件

def trans(filename):
    f=open(filename,'r',encoding='utf-8')
    s=f.read()
    f.close()
    f=open("单词.txt",'w',encoding='utf-8')
    f.write(s)
    f.close()
if v.get()==1:
    trans(r'CET-4.txt')
elif v.get()==2:
    trans(r'CET-6.txt')
else:
    trans(r'PG.txt')

定义Next函数,背下一个单词

def Next(*args):
    f2=open('选取单词.txt','r',encoding='utf-8')
    s2=f2.read()
    s2=s2.strip()                        #strip()方法去除文本前后的换行符
    f2.close()
    words=s2.split('\n')                 #将单词放入名为words的列表中
    if len(words)>0:
        f3=open('复习.txt','a+',encoding='utf-8')  #弹出words中的第一个单词,并放入名为"复习"的文件中
        str3=words.pop(0)+'\n'
        f3.write(str3)
        f3.close()
        f2=open('选取单词.txt','w',encoding='utf-8')  #去掉"选取单词"文件中的第一个单词
        str2='\n'.join(words)
        f2.write(str2)
        f2.close()
    if int(i.get())==int(n.get()):       #表明背完了单词
        messagebox.showinfo("提示","你已经背完了,快去复习吧!")   #弹出小窗口
    else:
        word=words[0].split('/')      #word中包含单词,音标和词义
        a1.set(word[0])               #显示下一个单词
        a2.set(word[1])
        a3.set(word[2])
        k=int(i.get())
        i.set(str(k+1))               #i的值增加1

定义delete函数,删除过于简单的单词

def delete(*args):
    f2=open('选取单词.txt','r',encoding='utf-8')
    s2=f2.read()
    s2=s2.strip()                       #strip()方法去除文本前后的换行符
    f2.close()
    words=s2.split('\n')                #将单词放入名为words的列表中
    str4=words.pop(0)+'\n'
    f4=open('斩.txt','a+',encoding='utf-8')  #打开文件"斩",将"选取单词"文件中的第一个单词添加进去
    f4.write(str4)
    f4.close()
    if int(i.get())==int(n.get()):       #表明背完了单词
        messagebox.showinfo("提示","你已经背完了,快去复习吧!")   #弹出小窗口
    else:
        word=words[0].split('/')      #word中包含单词,音标和词义
        a1.set(word[0])               #显示下一个单词
        a2.set(word[1])
        a3.set(word[2])
        k=int(i.get())
        i.set(str(k+1))               #i的值增加1
        f2=open('选取单词.txt','w',encoding='utf-8')  #去掉"选取单词"文件中的第一个单词
        str2='\n'.join(words)
        f2.write(str2)
        f2.close()

定义dif函数,把较难的词汇加入“难词.txt”

def dif(*args):
    string=a1.get()+'/'+a2.get()+'/'+a3.get()+'\n'
    f=open('难词.txt','a+',encoding='utf-8')
    f.write(string)
    f.close()

运行

开始

Python背单词代码 用python背单词_Python背单词代码

主界面

Python背单词代码 用python背单词_换行符_02

下一个

Python背单词代码 用python背单词_Python背单词代码_03

背完

Python背单词代码 用python背单词_tkinter_04

复习

Python背单词代码 用python背单词_tkinter_05

核对

Python背单词代码 用python背单词_tkinter_06

结束

Python背单词代码 用python背单词_换行符_07

查看结果

Python背单词代码 用python背单词_gui_08