在编写句典过程中,我基本采用了TDD(Test-driven development)方式,从一个小功能开始慢慢实现。
我的目标是编写一个这样的程序:
1,首先它是一个词典。可以在线查询英语单词。
2,其次它是一个阅读器,可以通过下拉菜单选择打开我所准备好的文档。文档包括英语例句和英语文章。
3,可以建立个人账号,设置密码,建立单词库。
4,可以进行生词复习,并且形成自己的学习曲线。
目前完成了前两个功能。
代码如下:
import tkinter as tkimport urllib, urllib.requestfrom tkinter import scrolledtextfrom tkinter import ttkimport tkinter.messagebox
def original_window():
enter_w = tk.Tk()
enter_w.title('宝贝登录呗')
enter_w.geometry('400x200')
lab_1 = tk.Label(enter_w,width=7,text='你叫啥',compound='center')
lab_1.place(x=50,y=80)
lab_2 = tk.Label(enter_w,width=7,text='暗号呢',compound='center')
lab_2.place(x=50,y=100)
global uesr_name,password
user_name = tk.StringVar()
password= tk.StringVar()
entry = tk.Entry(enter_w,textvariable=user_name)
entry.pack()
entry.place(x=150,y=80)
entry_1 = tk.Entry(enter_w,show="*",textvariable=password)
entry_1.pack()
entry_1.place(x=150,y=100)
def panduan(enter_w):
if entry.get() != 'zy' or entry_1.get() !='907890781':
tk.messagebox.showerror('*_*','暗号错了,你谁啊?')
else:
tk.messagebox.showinfo('^_^','来,学习开始啦')
new_window(enter_w)
btn = tk.Button(enter_w,text='想好了?',fg="black",width=7,compound='center',\
bg = "white",command = lambda :panduan(enter_w))
btn.pack()
btn.place(x=200,y=145)
enter_w.mainloop()
def new_window(enter_w): '''这个版本只有两个功能 一个是下拉菜单事件绑定 一个是搜索事件绑定 我想做的是让这两个事件各自拥有一个窗口,而不是共享同一个 如此一来 就实现了阅读文档同时查词的功能'''
#基本背景设定 cd = tk.Toplevel() cd.title('Jane') cd.geometry('700x800')
cd['background'] = 'pink'
photo = tk.PhotoImage(file = 'C:/Users/Austin/Desktop/bg3.gif') labelone = tk.Label(cd, image = photo, compound = tkinter.CENTER) labelone.place(x=15, y=50)
#建立两个下拉框 text1 = scrolledtext.ScrolledText(cd)
text2 = scrolledtext.ScrolledText(cd)
text3 = scrolledtext.ScrolledText(cd)
text1.place(x=285, y=550, width=400, height=200)
text3.place(x=285, y=50, width=400, height=450)
#以下是功能部分,首先是下拉菜单 zip = tkinter.StringVar() op = ttk.Combobox(cd, textvariable = zip) op['value'] = ('原因','假设','状态','结果','目的','对比','让步','强调') op.place(x=430, y=518) op.current(0)
#点击事件 def click(): f = open(r'C:\Users\Austin\Desktop\Zenglish\paragraphs\d.txt','r') s = f.read() text1.insert(tkinter.INSERT,s)
action = ttk.Button(cd, text = '查句子', command = click).place(x=600, y=518)
#这个是用下拉菜单搜索文章 zip = tkinter.StringVar() ed = ttk.Combobox(cd, textvariable = zip) ed['value'] = ('经济','科技','教育','职场','生活','身心','政治','旅行') ed.place(x=430, y=10) op.current(0)
#点击事件 def click(): f = open(r'C:\Users\Austin\Desktop\Zenglish\参考消息\教育\edu.txt','r') s = f.read() text3.insert(tkinter.INSERT,s)
action = ttk.Button(cd, text = '查文章', command = click).place(x=600, y=10)
#搜索事件,首先是输入框 word = tkinter.Entry(cd, bd = 9, font = ('微软雅黑', 13), fg = 'blue') word.place(x=15, y=300)
#搜索事件 def getword(): text2.delete(1.0, 'end') mot = word.get() with open(r'C:\Users\Austin\Desktop\all.txt','r',encoding='UTF8')as search: for line in search: if mot in line: text2.insert('insert',line)
text2.place(x=15, y=450, width= 250, height = 300)
tkinter.Button(cd, text = '让Jane告诉你', font=('微软雅黑',14), fg = 'black', command = getword).place(x=55, y= 360)
cd.mainloop()
original_window()