diff --git a/main(2).py b/main(2).py deleted file mode 100644 index a228d67..0000000 --- a/main(2).py +++ /dev/null @@ -1,219 +0,0 @@ -import tkinter as tk -from tkinter import messagebox, Menu, ttk -from googletrans import Translator -import sqlite3 - - -# 数据库初始化 -def init_db(): - conn = sqlite3.connect('users.db') - cursor = conn.cursor() - cursor.execute('''CREATE TABLE IF NOT EXISTS users - (id INTEGER PRIMARY KEY, username TEXT UNIQUE, password TEXT)''') - conn.commit() - conn.close() - - -# 注册用户 -def register_user(): - username = reg_username.get().strip() - password = reg_password.get().strip() - if not username or not password: - messagebox.showwarning("输入错误", "用户名和密码不能为空。") - return - conn = sqlite3.connect('users.db') - cursor = conn.cursor() - try: - cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password)) - conn.commit() - messagebox.showinfo("成功", "注册成功!") - reg_username.set("") - reg_password.set("") - switch_to_login() - except sqlite3.IntegrityError: - messagebox.showerror("错误", "用户名已存在。") - finally: - conn.close() - - -# 用户登录 -def login_user(): - username = login_username.get().strip() - password = login_password.get().strip() - if not username or not password: - messagebox.showwarning("输入错误", "用户名和密码不能为空。") - return - conn = sqlite3.connect('users.db') - cursor = conn.cursor() - cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password)) - user = cursor.fetchone() - conn.close() - if user: - messagebox.showinfo("成功", "登录成功!") - switch_to_main() - else: - messagebox.showerror("错误", "用户名或密码错误。") - - -# 切换到注册界面 -def switch_to_register(): - login_frame.pack_forget() - register_frame.pack(pady=20) - - -# 切换到登录界面 -def switch_to_login(): - register_frame.pack_forget() - login_frame.pack(pady=20) - - -# 切换到主界面 -def switch_to_main(): - login_frame.pack_forget() - main_frame.pack(pady=20) - - -# 切换回登录界面 -def switch_to_login_from_main(): - main_frame.pack_forget() - login_frame.pack(pady=20) - - -# 翻译文本 -def translate_text(): - src_lang = langs[source_lang.get()] - dest_lang = langs[target_lang.get()] - text = text_input.get("1.0", tk.END).strip() - if not text: - messagebox.showwarning("输入错误", "请输入要翻译的文本。") - return - translator = Translator(service_urls = ['translate.google.cn', 'translate.google.com']) - try: - translation = translator.translate(text, src=src_lang, dest=dest_lang) - text_output.delete("1.0", tk.END) - text_output.insert(tk.END, translation.text) - status_var.set("翻译成功") - except Exception as e: - messagebox.showerror("翻译错误", str(e)) - status_var.set("翻译失败") - - -# 清除文本 -def clear_text(): - text_input.delete("1.0", tk.END) - text_output.delete("1.0", tk.END) - status_var.set("文本已清除") - - -# 显示关于窗口 -def show_about(): - messagebox.showinfo("关于", "多语言翻译器\n版本: 2.0\n作者: 鲁迅") - - -if __name__ == "__main__": - # 初始化数据库 - init_db() - - # 创建主窗口 - root = tk.Tk() - root.title("多语言翻译器") - root.geometry("500x600") - - # 创建菜单 - menu = Menu(root) - root.config(menu=menu) - - # 添加文件菜单 - file_menu = Menu(menu, tearoff=0) - menu.add_cascade(label="文件", menu=file_menu) - file_menu.add_command(label="清除文本", command=clear_text) - file_menu.add_separator() - file_menu.add_command(label="退出", command=root.quit) - - # 添加帮助菜单 - help_menu = Menu(menu, tearoff=0) - menu.add_cascade(label="帮助", menu=help_menu) - help_menu.add_command(label="关于", command=show_about) - - # 登录框架 - login_frame = tk.Frame(root) - tk.Label(login_frame, text="用户名:").pack(pady=5) - login_username = tk.StringVar() - tk.Entry(login_frame, textvariable=login_username).pack(pady=5) - tk.Label(login_frame, text="密码:").pack(pady=5) - login_password = tk.StringVar() - tk.Entry(login_frame, textvariable=login_password, show="*").pack(pady=5) - tk.Button(login_frame, text="登录", command=login_user).pack(pady=5) - tk.Button(login_frame, text="注册", command=switch_to_register).pack(pady=5) - - # 注册框架 - register_frame = tk.Frame(root) - tk.Label(register_frame, text="用户名:").pack(pady=5) - reg_username = tk.StringVar() - tk.Entry(register_frame, textvariable=reg_username).pack(pady=5) - tk.Label(register_frame, text="密码:").pack(pady=5) - reg_password = tk.StringVar() - tk.Entry(register_frame, textvariable=reg_password, show="*").pack(pady=5) - tk.Button(register_frame, text="注册", command=register_user).pack(pady=5) - tk.Button(register_frame, text="返回登录", command=switch_to_login).pack(pady=5) - - # 主界面框架 - main_frame = tk.Frame(root) - - # 创建语言选择框 - langs = {'中文': 'zh-CN', '英文': 'en', '日文': 'ja', '韩文': 'ko', '法文': 'fr', '德文': 'de'} - - label_source = tk.Label(main_frame, text="选择源语言:") - label_source.pack(pady=5) - source_lang = ttk.Combobox(main_frame, values=list(langs.keys())) - source_lang.set('中文') - source_lang.pack(pady=5) - - label_target = tk.Label(main_frame, text="选择目标语言:") - label_target.pack(pady=5) - target_lang = ttk.Combobox(main_frame, values=list(langs.keys())) - target_lang.set('英文') - target_lang.pack(pady=5) - - # 创建输入文本框 - label_input = tk.Label(main_frame, text="请输入文本:") - label_input.pack(pady=5) - - text_input = tk.Text(main_frame, height=5, wrap=tk.WORD) - text_input.pack(pady=5) - - # 创建翻译按钮 - translate_button = tk.Button(main_frame, text="翻译", command=translate_text) - translate_button.pack(pady=5) - - # 创建清除按钮 - clear_button = tk.Button(main_frame, text="清除", command=clear_text) - clear_button.pack(pady=5) - - # 创建输出文本框 - label_output = tk.Label(main_frame, text="翻译结果:") - label_output.pack(pady=5) - - text_output = tk.Text(main_frame, height=5, wrap=tk.WORD) - text_output.pack(pady=5) - - # 创建状态栏 - status_var = tk.StringVar() - status_var.set("欢迎使用多语言翻译器") - status_bar = tk.Label(root, textvariable=status_var, relief=tk.SUNKEN, anchor=tk.W) - status_bar.pack(fill=tk.X, side=tk.BOTTOM, ipady=2) - - # 美化界面 - root.option_add("*Font", "Arial 12") - root.configure(bg="#f0f0f0") - - # 设置支持的组件背景颜色 - for widget in root.winfo_children(): - if isinstance(widget, tk.Label) or isinstance(widget, tk.Button) or isinstance(widget, tk.Text): - widget.configure(bg="#f0f0f0") - - # 初始显示登录界面 - login_frame.pack(pady=20) - - # 运行主循环 - root.mainloop()