代码
import requests,json,time
import tkinter as tk
root = tk.Tk()
ciku_type = [["GMAT", "GMAT"], ["NGEE", "考研"], ["NCEE", "高考"], ["CET4", "四级"], ["CET6", "六级"], [
"TEM", "英专"], ["TOEFL", "托福"], ["GRE", "GRE"], ["IELTS", "雅思"], ["NONE", "任意"]]
va = tk.IntVar()
for i in range(50):
locals()['v'+str(i)] = tk.IntVar()
vlist = [v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24,
v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49]
for i in range(50):
locals()['var'+str(i)] = tk.IntVar()
varlist = [var0, var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14, var15, var16, var17, var18, var19, var20, var21, var22, var23, var24,
var25, var26, var27, var28, var29, var30, var31, var32, var33, var34, var35, var36, var37, var38, var39, var40, var41, var42, var43, var44, var45, var46, var47, var48, var49]
def callCheckbutton():
global danci,words_knows,not_knows,word_ranks
danci = []
words_knows = []
not_knows = []
word_ranks = []
m = 0
for x in words['data']:
if vlist[m].get()==True:
danci.append(x['content'])
words_knows.append(x)
word_ranks.append(str(x['rank']))
else:
not_knows.append(x)
word_ranks.append(str(x['rank']))
m = m+1
def callCheckbutton_2():
global danci,words_knows,not_knows,word_ranks
danci = []
words_knows = []
not_knows = []
word_ranks = []
if va.get()==True:
m = 0
for x in words['data']:
danci.append(x['content'])
words_knows.append(x)
word_ranks.append(str(x['rank']))
vlist[m].set(1)
m = m+1
else:
m = 0
for x in words['data']:
not_knows.append(x)
word_ranks.append(str(x['rank']))
vlist[m].set(0)
m = m+1
class firstpage(object):
def __init__(self, master=None):
self.root = master
self.page = tk.Frame(self.root)
self.page.pack()
tk.Label(self.page, text=u'评估过程包括三步:').grid(sticky='W')
tk.Label(self.page, text=u'1. 选择适合你的出题范围').grid(sticky='W')
tk.Label(self.page, text=u'2. 通过50个单词得到你的大致词汇量范围').grid(sticky='W')
tk.Label(self.page, text=u'3. 检验你是否真的掌握你在第二步中选择的单词').grid(sticky='W')
tk.Button(self.page, text=u'下一步',command=self.secpage).grid()
def secpage(self):
self.page.destroy()
mainpage(self.root)
class mainpage(object):
def __init__(self, master=None):
self.root = master
self.page = tk.Frame(self.root)
self.page.pack()
tk.Label(self.page, text=u'第 1 步,请选择出题范围\n').grid(columnspan=5,sticky='W')
tk.Button(self.page, text=u' GMAT ',command=self.secpage0).grid(column=0,row=1,sticky='EW')
tk.Button(self.page, text=u' 考研 ',command=self.secpage1).grid(column=1,row=1,sticky='EW')
tk.Button(self.page, text=u' 高考 ',command=self.secpage2).grid(column=2,row=1,sticky='EW')
tk.Button(self.page, text=u' 四级 ',command=self.secpage3).grid(column=3,row=1,sticky='EW')
tk.Button(self.page, text=u' 六级 ',command=self.secpage4).grid(column=4,row=1,sticky='EW')
tk.Button(self.page, text=u' 英专 ',command=self.secpage5).grid(column=0,row=2,sticky='EW')
tk.Button(self.page, text=u' 托福 ',command=self.secpage6).grid(column=1,row=2,sticky='EW')
tk.Button(self.page, text=u' GRE ',command=self.secpage7).grid(column=2,row=2,sticky='EW')
tk.Button(self.page, text=u' 雅思 ',command=self.secpage8).grid(column=3,row=2,sticky='EW')
tk.Button(self.page, text=u' 任意 ',command=self.secpage9).grid(column=4,row=2,sticky='EW')
def secpage0(self):
global ciku,words
ciku = ciku_type[0][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage1(self):
global ciku
ciku = ciku_type[1][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage2(self):
global ciku
ciku = ciku_type[2][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage3(self):
global ciku
ciku = ciku_type[3][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage4(self):
global ciku
ciku = ciku_type[4][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage5(self):
global ciku
ciku = ciku_type[5][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage6(self):
global ciku
ciku = ciku_type[6][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage7(self):
global ciku
ciku = ciku_type[7][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage8(self):
global ciku
ciku = ciku_type[8][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
def secpage9(self):
global ciku
ciku = ciku_type[9][0]
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
global words
words = json.loads(test.text)
self.page.destroy()
secondpage(self.root)
class secondpage(object):
def __init__(self, master=None):
self.root = master
self.page = tk.Frame(self.root)
self.page.pack()
tk.Label(self.page, text=u'第 2 步,请选择你认识的单词\n').grid(columnspan=5,sticky='W')
tk.Checkbutton(self.page,
variable = va,
text = '全选',
command = callCheckbutton_2).grid(column=9,sticky='E',row=0)
n = 0
for i in words['data']:
if n<10:
tk.Checkbutton(self.page,
variable = vlist[n],
text = i['content'],
command = callCheckbutton).grid(column=n,sticky='W',row=1)
elif n<20:
tk.Checkbutton(self.page,
variable = vlist[n],
text = i['content'],
command = callCheckbutton).grid(column=n-10,sticky='W',row=2)
elif n<30:
tk.Checkbutton(self.page,
variable = vlist[n],
text = i['content'],
command = callCheckbutton).grid(column=n-20,sticky='W',row=3)
elif n<40:
tk.Checkbutton(self.page,
variable = vlist[n],
text = i['content'],
command = callCheckbutton).grid(column=n-30,sticky='W',row=4)
else :
tk.Checkbutton(self.page,
variable = vlist[n],
text = i['content'],
command = callCheckbutton).grid(column=n-40,sticky='W',row=5)
n = n+1
self.Button = tk.Button(self.page, text=u'下一步',command=self.thipage)
self.Button.grid(columnspan=5,sticky='E')
def thipage(self):
self.page.destroy()
thirdpage(self.root)
class thirdpage(object):
def __init__(self, master=None):
self.root = master
self.page = tk.Frame(self.root)
self.page.pack()
tk.Label(self.page, text=u'第 3 步,单词测试,请选择正确的词义\n').grid(columnspan=5,sticky='W')
global right_ranks,wrong_words,right_num,slaves01,slaves11,slaves12,slaves13,slaves14,s
right_ranks = []
wrong_words = []
right_num = 0
slaves01 = []
slaves11 = []
slaves12 = []
slaves13 = []
slaves14 = []
n = 0
for y in words_knows:
tk.Label(self.page, text=y['content']).grid(sticky='NS',row=1,column=0,rowspan=4,ipadx=50)
tk.Button(self.page,anchor=tk.W,
text='A:'+y['definition_choices'][0]['definition'],
command=self.forgetButton0).grid(sticky='EW',row=1,column=1)
tk.Button(self.page,anchor=tk.W,
text='B:'+y['definition_choices'][1]['definition'],
command=self.forgetButton1).grid(sticky='EW',row=2,column=1)
tk.Button(self.page,anchor=tk.W,
text='C:'+y['definition_choices'][2]['definition'],
command=self.forgetButton2).grid(sticky='EW',row=3,column=1)
tk.Button(self.page,anchor=tk.W,
text='D:'+y['definition_choices'][3]['definition'],
command=self.forgetButton3).grid(sticky='EW',row=4,column=1)
if n<len(words_knows)-1:
slaves01.append(self.page.grid_slaves(1,0)[0])
slaves11.append(self.page.grid_slaves(1,1)[0])
slaves12.append(self.page.grid_slaves(2,1)[0])
slaves13.append(self.page.grid_slaves(3,1)[0])
slaves14.append(self.page.grid_slaves(4,1)[0])
self.page.grid_slaves(1,0)[0].grid_forget()
self.page.grid_slaves(1,1)[0].grid_forget()
self.page.grid_slaves(2,1)[0].grid_forget()
self.page.grid_slaves(3,1)[0].grid_forget()
self.page.grid_slaves(4,1)[0].grid_forget()
n = n+1
s = len(slaves01)
def forgetButton0(self):
global right_ranks,wrong_words,right_num,words_knows,s
y = words_knows.pop()
if y['definition_choices'][0]['rank']==y['rank']:
right_num = right_num+1
right_ranks.append(str(y['rank']))
else:
wrong_words.append(y)
if s>0:
self.page.grid_slaves(1,0)[0].grid_forget()
self.page.grid_slaves(1,1)[0].grid_forget()
self.page.grid_slaves(2,1)[0].grid_forget()
self.page.grid_slaves(3,1)[0].grid_forget()
self.page.grid_slaves(4,1)[0].grid_forget()
slaves01[s-1].grid(sticky='NS',row=1,column=0,rowspan=4,ipadx=50)
slaves11[s-1].grid(sticky='EW',row=1,column=1)
slaves12[s-1].grid(sticky='EW',row=2,column=1)
slaves13[s-1].grid(sticky='EW',row=3,column=1)
slaves14[s-1].grid(sticky='EW',row=4,column=1)
else:
self.page.destroy()
fourthpage(self.root)
s = s-1
def forgetButton1(self):
global right_ranks,wrong_words,right_num,words_knows,s
y = words_knows.pop()
if y['definition_choices'][1]['rank']==y['rank']:
right_num = right_num+1
right_ranks.append(str(y['rank']))
else:
wrong_words.append(y)
if s>0:
self.page.grid_slaves(1,0)[0].grid_forget()
self.page.grid_slaves(1,1)[0].grid_forget()
self.page.grid_slaves(2,1)[0].grid_forget()
self.page.grid_slaves(3,1)[0].grid_forget()
self.page.grid_slaves(4,1)[0].grid_forget()
slaves01[s-1].grid(sticky='NS',row=1,column=0,rowspan=4,ipadx=50)
slaves11[s-1].grid(sticky='EW',row=1,column=1)
slaves12[s-1].grid(sticky='EW',row=2,column=1)
slaves13[s-1].grid(sticky='EW',row=3,column=1)
slaves14[s-1].grid(sticky='EW',row=4,column=1)
else:
self.page.destroy()
fourthpage(self.root)
s = s-1
def forgetButton2(self):
global right_ranks,wrong_words,right_num,words_knows,s
y = words_knows.pop()
if y['definition_choices'][2]['rank']==y['rank']:
right_num = right_num+1
right_ranks.append(str(y['rank']))
else:
wrong_words.append(y)
if s>0:
self.page.grid_slaves(1,0)[0].grid_forget()
self.page.grid_slaves(1,1)[0].grid_forget()
self.page.grid_slaves(2,1)[0].grid_forget()
self.page.grid_slaves(3,1)[0].grid_forget()
self.page.grid_slaves(4,1)[0].grid_forget()
slaves01[s-1].grid(sticky='NS',row=1,column=0,rowspan=4,ipadx=50)
slaves11[s-1].grid(sticky='EW',row=1,column=1)
slaves12[s-1].grid(sticky='EW',row=2,column=1)
slaves13[s-1].grid(sticky='EW',row=3,column=1)
slaves14[s-1].grid(sticky='EW',row=4,column=1)
else:
self.page.destroy()
fourthpage(self.root)
s = s-1
def forgetButton3(self):
global right_ranks,wrong_words,right_num,words_knows,s
y = words_knows.pop()
if y['definition_choices'][3]['rank']==y['rank']:
right_num = right_num+1
right_ranks.append(str(y['rank']))
else:
wrong_words.append(y)
if s>0:
self.page.grid_slaves(1,0)[0].grid_forget()
self.page.grid_slaves(1,1)[0].grid_forget()
self.page.grid_slaves(2,1)[0].grid_forget()
self.page.grid_slaves(3,1)[0].grid_forget()
self.page.grid_slaves(4,1)[0].grid_forget()
slaves01[s-1].grid(sticky='NS',row=1,column=0,rowspan=4,ipadx=50)
slaves11[s-1].grid(sticky='EW',row=1,column=1)
slaves12[s-1].grid(sticky='EW',row=2,column=1)
slaves13[s-1].grid(sticky='EW',row=3,column=1)
slaves14[s-1].grid(sticky='EW',row=4,column=1)
else:
self.page.destroy()
fourthpage(self.root)
s = s-1
class fourthpage(object):
def __init__(self, master=None):
self.root = master
self.page = tk.Frame(self.root)
self.page.pack()
url = 'https://www.shanbay.com/api/v1/vocabtest/vocabularies/'
headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}
data_ranks = {'category': ciku,
'phase': '',
'right_ranks': ','.join(right_ranks),
'word_ranks': ','.join(word_ranks)}
res_vocab = requests.post(url,headers=headers,data=data_ranks)
js_vocab = json.loads(res_vocab.text)
tk.Label(self.page, text='你的词汇量大约是:'+str(js_vocab['data']['vocab'])).grid()
tk.Label(self.page, text=js_vocab['data']['comment']).grid()
tk.Label(self.page, text='在上述'+str(len(words['data']))+'个单词中,有'+str(len(danci))+'个是你认识的,有'+str(right_num)+'个是你掌握的,有'+str(len(wrong_words))+'个是你记错的').grid()
self.Button = tk.Button(self.page, text=u'退出',command=self.quit)
self.Button.grid()
def quit(self):
self.page.quit()
firstpage(root)
root.title('5分钟,评估你的单词量')
root.mainloop()
效果