一个自行输入记录的词典工具

import json
import tkinter as tk


def load_database():
    try:
        with open('database1.json', 'r') as file:
            return json.load(file)
    except FileNotFoundError:
        return {}


def save_database(database):
    with open('database1.json', 'w') as file:
        json.dump(database, file)


def add_definition():
    term = term_entry.get()
    definition = definition_entry.get()

    if not term:
        status_label.config(text="请输入词条。")
        return
    if not definition:
        status_label.config(text="请输入释义。")
        return
    database[term] = definition
    save_database(database)

    status_label.config(text="已添加至数据库。")


def get_definition():
    term = term_entry.get()
    definition = definition_entry.get()

    if not term:
        status_label.config(text="请输入词条。")
        return

    matching_terms = []
    for key in database.keys():
        if term in key and definition in database[key]:
            matching_terms.append((key, database[key]))

    if matching_terms:
        status_label.config(text=f"以下是符合条件的词条:")
        output_text.delete("1.0", tk.END)
        for term, definition in matching_terms:
            output_text.tag_configure("term_style", font=("Helvetica", 14), foreground="red")
            output_text.insert(tk.END, term, "term_style")
            output_text.insert(tk.END, ": \n")
            output_text.tag_configure("definition_style", font=("Microsoft YaHei", 12), foreground="blue")
            output_text.insert(tk.END, definition, "definition_style")

            output_text.insert(tk.END, "\n\n")
    else:
        status_label.config(text="未找到符合条件的词条。")


def edit_definition():
    term = term_entry.get()
    new_definition = definition_entry.get()

    if not term:
        status_label.config(text="请输入词条。")
        return

    if not new_definition:
        status_label.config(text="请输入释义。")
        return

    if term in database:
        database[term] = new_definition
        save_database(database)
        status_label.config(text="已修改词条。")
    else:
        status_label.config(text="该词条不存在。")


def delete_term():
    term = term_entry.get()

    if not term:
        status_label.config(text="请输入词条。")
        return

    if term in database:
        del database[term]
        save_database(database)
        status_label.config(text="已删除词条。")
    else:
        status_label.config(text="该词条不存在。")

def clear_input1():
    term_entry.delete(0, tk.END)
def clear_input2():
    definition_entry.delete(0, tk.END)

def clear_output():
    output_text.delete("1.0", tk.END)

def show_all():
    sorted_terms = sorted(database.keys(), key=lambda x: x.lower())
    output_text.delete("1.0", tk.END)
    for term in sorted_terms:
        definition = database[term]
        # 设置 term 的样式:字体大小为 14,颜色为红色
        output_text.tag_configure("term_style", font=("Helvetica", 14), foreground="red")
        output_text.insert(tk.END, term, "term_style")
        output_text.insert(tk.END, ": \n")
        # 设置 definition 的样式:字体大小为 12,颜色为蓝色
        output_text.tag_configure("definition_style", font=("Microsoft YaHei", 12), foreground="blue")
        output_text.insert(tk.END, definition, "definition_style")

        output_text.insert(tk.END, "\n\n")


# 加载数据库
database = load_database()

# 创建窗口
window = tk.Tk()
window.title("词典")
window.configure(bg="white")

# 创建标签和文本框用于输入名词# 创建输入框和清除按钮
input_frame = tk.Frame(window, bg="white")
input_frame.pack(padx=10, pady=10)
term_label = tk.Label(input_frame, text="名词:", bg="white", font=("Microsoft YaHei", 12))
term_label.pack(side=tk.LEFT)
term_entry = tk.Entry(input_frame, font=("Microsoft YaHei", 12), width=50)
term_entry.pack(side=tk.LEFT)
clear_input_button = tk.Button(input_frame, text="清空", command=clear_input1, bg="lightblue", font=("Microsoft YaHei", 12))
clear_input_button.pack(side=tk.LEFT)

# 创建标签和文本框用于输入释义
input_frame = tk.Frame(window, bg="white")
input_frame.pack(padx=10, pady=10)
definition_label = tk.Label(input_frame, text="释义:", bg="white", font=("Microsoft YaHei", 12))
definition_label.pack(side=tk.LEFT)
definition_entry = tk.Entry(input_frame, font=("Microsoft YaHei", 12), width=50)
definition_entry.pack(side=tk.LEFT)
clear_input_button = tk.Button(input_frame, text="清空", command=clear_input2, bg="lightblue", font=("Microsoft YaHei", 12))
clear_input_button.pack(side=tk.LEFT)


# 创建按钮并绑定对应的函数
add_button = tk.Button(window, text="添加词条", command=add_definition, bg="lightblue", font=("Microsoft YaHei", 12))
add_button.pack()

get_button = tk.Button(window, text="查找词条", command=get_definition, bg="lightblue", font=("Microsoft YaHei", 12))
get_button.pack()

edit_button = tk.Button(window, text="修改词条", command=edit_definition, bg="lightblue", font=("Microsoft YaHei", 12))
edit_button.pack()

delete_button = tk.Button(window, text="删除词条", command=delete_term, bg="lightblue", font=("Microsoft YaHei", 12))
delete_button.pack()

show_button = tk.Button(window, text="显示所有词条", command=show_all, bg="lightblue", font=("Microsoft YaHei", 12))
show_button.pack()

# 创建状态标签和文本框用于输出信息
status_label = tk.Label(window, text="", bg="white", font=("Microsoft YaHei", 12))
status_label.pack()
output_frame = tk.Frame(window, bg="white")
output_frame.pack(padx=10, pady=(0, 10))
output_text = tk.Text(output_frame, width=70, height=20, font=("Microsoft YaHei", 12))
output_text.pack(side=tk.LEFT)
clear_output_button = tk.Button(output_frame, text="清空", command=clear_output, bg="lightblue", font=("Microsoft YaHei", 12))
clear_output_button.pack(side=tk.LEFT)
# 设置窗口的初始大小和位置
window.geometry("700x600")
window.resizable(True, True)

# 运行主循环
window.mainloop()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值