|
|

楼主 |
发表于 2026-5-11 06:35:24
|
显示全部楼层
本帖最后由 ling830 于 2026-5-11 06:39 编辑
增加 夜间模式
- import sys
- from tkinter import *
- # 1. 兼容性导入处理
- try:
- from tkinter import filedialog as tkFileDialog
- from tkinter import messagebox
- except ImportError:
- import tkFileDialog
- import tkMessageBox as messagebox
- # 2. 初始化主窗口
- root = Tk()
- root.title("Python 极简编辑器 - 最终全功能版")
- root.geometry("900x750")
- # 全局状态变量
- current_font_family = "Microsoft YaHei"
- current_font_size = 12
- current_file_path = None
- is_dark_mode = False
- # 3. 功能函数
- def update_font():
- """统一更新文本框字体"""
- text.config(font=(current_font_family, current_font_size))
- def toggle_night_mode():
- """一键切换夜间模式"""
- global is_dark_mode
- if not is_dark_mode:
- # 切换到夜间模式 (VS Code 风格)
- text.config(bg="#1e1e1e", fg="#d4d4d4", insertbackground="white")
- button_frame.config(bg="#2d2d2d")
- status_label.config(bg="#2d2d2d", fg="#858585")
- night_btn.config(text="🌞 白天模式", bg="#3c3c3c", fg="white")
- is_dark_mode = True
- else:
- # 切换回白天模式
- text.config(bg="#ffffff", fg="#000000", insertbackground="black")
- button_frame.config(bg="#f0f0f0")
- status_label.config(bg="#f0f0f0", fg="gray")
- night_btn.config(text="🌙 夜间模式", bg="#e1e1e1", fg="black")
- is_dark_mode = False
- def open_file(event=None):
- global current_file_path
- path = tkFileDialog.askopenfilename(
- filetypes=[("文本文件", "*.txt"), ("Python 脚本", "*.py"), ("所有文件", "*.*")]
- )
- if path:
- try:
- with open(path, "r", encoding="utf-8") as file: content = file.read()
- except:
- with open(path, "r", encoding="gbk") as file: content = file.read()
- text.delete("1.0", END)
- text.insert("1.0", content)
- current_file_path = path
- root.title(f"正在编辑: {path}")
- return "break"
- def save_file(event=None):
- global current_file_path
- if current_file_path:
- try:
- t = text.get("1.0", "end-1c")
- with open(current_file_path, "w", encoding="utf-8") as file:
- file.write(t)
- root.title(f"已保存: {current_file_path}")
- except Exception as e:
- messagebox.showerror("错误", f"保存失败: {e}")
- else:
- save_as()
- return "break"
- def save_as():
- global current_file_path
- t = text.get("1.0", "end-1c")
- path = tkFileDialog.asksaveasfilename(
- defaultextension=".txt",
- filetypes=[("文本文件", "*.txt"), ("Python 脚本", "*.py"), ("HTML 网页", "*.html"), ("所有文件", "*.*")]
- )
- if path:
- with open(path, "w", encoding="utf-8") as file:
- file.write(t)
- current_file_path = path
- root.title(f"正在编辑: {path}")
- def change_font_family(name):
- global current_font_family
- current_font_family = name
- update_font()
- def mouse_wheel_font_size(event):
- global current_font_size
- if event.delta > 0:
- current_font_size += 1
- else:
- if current_font_size > 5:
- current_font_size -= 1
- update_font()
- # 4. UI 布局
- root.grid_rowconfigure(0, weight=1)
- root.grid_columnconfigure(0, weight=1)
- # 文本框 (支持撤销和自动换行)
- text = Text(root, font=(current_font_family, current_font_size), undo=True, wrap=WORD)
- text.grid(row=0, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)
- # 滚动条
- scroll = Scrollbar(text)
- text.configure(yscrollcommand=scroll.set)
- scroll.pack(side=RIGHT, fill=Y)
- # 底部按钮区
- button_frame = Frame(root, bd=1, relief=SUNKEN)
- button_frame.grid(row=1, column=0, columnspan=2, pady=5, sticky="ew")
- # 左侧功能组
- Button(button_frame, text="打开 (Ctrl+O)", command=open_file).pack(side=LEFT, padx=5)
- Button(button_frame, text="保存 (Ctrl+S)", command=save_file).pack(side=LEFT, padx=5)
- Button(button_frame, text="另存为", command=save_as).pack(side=LEFT, padx=5)
- # --- 重新找回的字体菜单 ---
- font_btn = Menubutton(button_frame, text="选择字体", relief=RAISED)
- font_btn.pack(side=LEFT, padx=5)
- font_menu = Menu(font_btn, tearoff=0)
- font_btn.config(menu=font_menu)
- font_list = [
- ("微软雅黑", "Microsoft YaHei"),
- ("宋体", "SimSun"),
- ("楷体", "KaiTi"),
- ("Consolas", "Consolas"),
- ("Arial", "Arial")
- ]
- for label, name in font_list:
- font_menu.add_command(label=label, command=lambda n=name: change_font_family(n))
- # 右侧夜间模式按钮
- night_btn = Button(button_frame, text="🌙 夜间模式", command=toggle_night_mode)
- night_btn.pack(side=RIGHT, padx=10)
- # 5. 绑定快捷键
- root.bind("<Control-o>", open_file)
- root.bind("<Control-O>", open_file)
- root.bind("<Control-s>", save_file)
- root.bind("<Control-S>", save_file)
- text.bind("<Control-MouseWheel>", mouse_wheel_font_size)
- # 底部状态栏
- status_label = Label(root, text=" 提示: Ctrl+O 打开 | Ctrl+S 保存 | Ctrl+滚轮 缩放字体", fg="gray", anchor="w")
- status_label.grid(row=2, column=0, columnspan=2, sticky="ew")
- root.mainloop()
复制代码 |
|