之前在一个公众号看到一个用来测试单词的小应用,使用web应用实现的,于是,结合最近学的Python的tkinter模块,给它做了一个窗体,还添加了 一些其他的小功能。
下面进行代码说明:
代码由两部分组成:生成要测试的单词的部分和将待测试展示在窗体的部分
第一部分:对生成测试单词的类进行封装,包括类的私有变量、初始化、以及单词生成函数
#定义选择cet4的单词的类
import os
import random
import time
class EnglishWord:
__wordData = []
def __init__(self):
path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(path,'material','cet4.txt'),encoding='utf-8') as fp:
self.__wordData = fp.readlines()
def chooseWord(self,n,s):
random.seed(s)
data = self.__wordData
return random.choices(data,k=n)
第二部分:窗体设计以及显示,这一部分是花了时间比较久的部分,原因在于没弄懂tkinter中Button的用法原理
import tkinter as tk
import random
import re
#random.seed(9)
from wordClass import EnglishWord
#import threading as td
color = ['lightgray','white']
hit_bool = [1,1,1,1,1,1,1,1]#[0,0,0,0,0,0,0,0]
wlist = []
ilist = []
lb = []
bt = []
def choseWord(s):
global wlist,ilist
wordList = EnglishWord().chooseWord(8,s)
wlist.clear()
ilist.clear()
pat1 = "(.*?)/.*"
pat2 = ".*?(/.*?)\n"
for i in range(8):
wlist.append(re.compile(pat1).findall(wordList[i]))
ilist.append(re.compile(pat2).findall(wordList[i]))
clic=1
def freshWord():
global clic,hit_bool
clic=clic+1
choseWord(clic)
#print(len(wlist))
#print(wlist)
for i in range(8):
lb[i].config(text=ilist[i])
hit_bool = [1, 1, 1, 1, 1, 1, 1, 1]
def hideWord():
global wlist, ilist,hit_bool
for i in range(8):
lb[i].config(text=ilist[i])
hit_bool = [1, 1, 1, 1, 1, 1, 1, 1]
def showWord(i):
global hit_bool,wlist,ilist
#print(i)#7
if hit_bool[i]==1:
lb[i].config(text=wlist[i])
hit_bool[i] = 0
#print('h')#不打印
else:
lb[i].config(text=ilist[i])
hit_bool[i] = 1
#print('h')
if __name__=='__main__':
choseWord(1)
window = tk.Tk()
window.title('CET4')
window.geometry('600x450')
xlb = 130
ylb = 40
xbt = xlb+270
ybt = ylb
for i in range(8):
if i % 2 == 0:
lbColor = color[0]
else:
lbColor = color[1]
lb.append(tk.Label(window, text=ilist[i], bg=lbColor, height=2, width=36))
lb[i].place(x=xlb, y=ylb)
ylb = ylb + 45
tk.Button(window, text='查看', height=1, width=5, command=lambda: showWord(0)).place(x=xbt, y=ybt)
ybt = ybt + 46
tk.Button(window, text='查看', height=1, width=5, command=lambda: showWord(1)).place(x=xbt, y=ybt)
ybt = ybt + 46
tk.Button(window, text='查看', height=1, width=5, command=lambda: showWord(2)).place(x=xbt, y=ybt)
ybt = ybt + 46
tk.Button(window, text='查看', height=1, width=5, command=lambda: showWord(3)).place(x=xbt, y=ybt)
ybt = ybt + 46
tk.Button(window, text='查看', height=1, width=5, command=lambda: showWord(4)).place(x=xbt, y=ybt)
ybt = ybt + 46
tk.Button(window, text='查看', height=1, width=5, command=lambda: showWord(5)).place(x=xbt, y=ybt)
ybt = ybt + 46
tk.Button(window, text='查看', height=1, width=5, command=lambda: showWord(6)).place(x=xbt, y=ybt)
ybt = ybt + 46
tk.Button(window, text='查看', height=1, width=5, command=lambda: showWord(7)).place(x=xbt, y=ybt)
ybt = ybt + 46
#添加全部单词恢复成音标+释义的功能
tk.Button(window, text='恢复', height=1, width=5, command=hideWord).place(x=xbt, y=ybt)
# 添加刷新每次记忆的单词的功能
tk.Button(window, text='刷新', height=1, width=5, command=freshWord).place(x=xbt+40, y=ybt)
"""
for i in range(8):
bt.append(tk.Button(window, text='查看',height=1,width=5,command=lambda:showWord(i)))
bt[i].place(x=xbt, y=ybt )
ybt = ybt + 46
"""
window.mainloop()
#print(td.current_thread())#<_MainThread(MainThread, started 1408)>
写第一部分代码几乎没出什么问题,但是,第二部分就遇到了一点问题。
(1)页面设置只展示8个单词,用到了tkinter中的label和button。本想用循环创建多个label和button对象,但是最后label只会展示最后一个单词,应该与button的点击函数以及工作原理有关,没找到好的解决办法,就重复使用创建button的代码(如果其他有好的办法解决,请赐教)
(2)伪随机数的问题。“刷新”按钮对应的功能是换8个单词,如果不改变seed(),就不能更换单词
界面展示:
1、初始界面
2、点击对应的“查看”按钮
3、点击“恢复”按钮
4、点击刷新按钮
以上就是这个小小的尝试,虽然我的代码很丑,但是不妨碍用来练习简单的tkinter