前言
今日分享 Python 制作英文学习词典,大家都可以尝试。
题目
制作英文学习词典。编写程序制作英文学习词典,词典有3个基本功能:添加、查询和退出。程序读取源文件路径下的txt格式词典文件,若没有就创建一个。词典文件存储方式为“英文单词 中文单词”,每行仅有一对中英释义。程序会根据用户的选择进入相应的功能模块,并显示相应的操作提示。当添加的单词已经存在时,显示“该单词已经添加到词典里”;当查询的单词不存在时,显示“字典库中未找到这个单词”。用户输入其他选项时候,提示“输入有误!”
代码
from tkinter import filedialog
import tkinter
def add_word():
print(dict_path)
add_word_window = tkinter.Tk()
label1 = tkinter.Label(add_word_window, text='英语单词:')
label1.grid(row=0, column=0)
label2 = tkinter.Label(add_word_window, text='中文单词:')
label2.grid(row=1, column=0)
global eng
global chi
eng = tkinter.Entry(add_word_window)
eng.grid(row=0, column=1, padx=10, pady=5)
chi = tkinter.Entry(add_word_window)
chi.grid(row=1, column=1, padx=10, pady=5)
btn = tkinter.Button(add_word_window,text="添加单词", command=opera_add_word_to_file).grid(row=2, column=1, padx=10, pady=5)
add_word_window.mainloop()
def opera_add_word_to_file():
file_preprocessor()
add_word_to_file_windows = tkinter.Tk()
english = eng.get()
if english not in words:
chinese = chi.get()
s = english + " " + chinese + '\n'
#print(s)
with open(dict_path, encoding="utf-8", mode="a") as file:
file.write(s)
label = tkinter.Label(add_word_to_file_windows, text='添加成功!')
label.grid(row=0, column=0, padx=10, pady=5)
button = tkinter.Button(add_word_to_file_windows, text='确定', command=add_word_to_file_windows.quit)
button.grid(row=1, column=0, sticky=tkinter.E, padx=30, pady=5)
else:
label = tkinter.Label(add_word_to_file_windows, text='该单词已添加到词典库!')
label.grid(row=0, column=0, padx=10, pady=5)
button = tkinter.Button(add_word_to_file_windows, text='确定', command=add_word_to_file_windows.quit)
button.grid(row=1, column=0, sticky=tkinter.E, padx=30, pady=5)
add_word_to_file_windows.mainloop()
def select_file():
global dict_path
dict_path = ""
dict_path = filedialog.askopenfilename()
return dict_path
def word_search():
word_search_ui = tkinter.Tk()
label_eng = tkinter.Label(word_search_ui, text='英语单词:')
label_eng.grid(row=0, column=0)
global e2c
e2c = tkinter.Entry(word_search_ui)
e2c.grid(row=0, column=1, padx=10, pady=5)
button = tkinter.Button(word_search_ui, text='确定', command=eng2chi_opera)
button.grid(row=0, column=2, sticky=tkinter.E, padx=30, pady=5)
word_search_ui.mainloop()
def eng2chi_opera():
child_ui = tkinter.Tk()
file_preprocessor()
english = e2c.get()
if english not in words:
# print("字典库中未找到这个单词!")
child_ui_tag = tkinter.Label(child_ui, text="字典库中未找到这个单词!").grid(row=0, column=0)
else:
# print("{}的中文释义是:{}".format(english, words[english]))
child_ui_tag = tkinter.Label(child_ui, text=words[english]).grid(row=0, column=0)
child_ui.mainloop()
def file_preprocessor():
count = 0
dict_read = open(dict_path, 'r', encoding="UTF-8")
for line in dict_read.readlines():
s = str(line).split()
words[s[0]] = s[1]
count += 1
dict_read.close()
if __name__ == "__main__":
global words
global count
words = {}
count = 0
main_window = tkinter.Tk()
btn0 = tkinter.Button(main_window, text="选择文件", command=select_file).grid(row=0, column=0, sticky=tkinter.E, padx=30, pady=5)
btn1 = tkinter.Button(main_window, text="添加", command=add_word).grid(row=0, column=1, sticky=tkinter.E, padx=30, pady=5)
btn2 = tkinter.Button(main_window, text="查询", command=word_search).grid(row=0, column=2, sticky=tkinter.E, padx=30, pady=5)
btn3 = tkinter.Button(main_window, text="退出", command=main_window.quit).grid(row=0, column=3, sticky=tkinter.E, padx=30, pady=5)
main_window.mainloop()