Python文本编辑器

代码:

import tkinter as tk
from tkinter import simpledialog
from tkinter import filedialog
from tkinter import font
from tkinter import colorchooser
from tkinter import ttk, colorchooser
import json
import pyperclip
import time

class TextEditor:
    def __init__(self, root):
        self.root = root
        self.root.title("文档编辑器")

        # 创建菜单
        self.create_menu()

        # 创建文本编辑区
        self.text = tk.Text(root)
        self.text.pack(expand="yes", fill="both")

        self.current_file = None
        

        self.context_menu = tk.Menu(self.root, tearoff=0)
        self.context_menu.add_command(label="全选", command=self.select_all)
        self.context_menu.add_command(label="复制", command=self.copy_text)
        self.context_menu.add_command(label="粘贴", command=self.paste_text)
        
        self.text.bind("<Button-3>", self.show_context_menu)

    def show_context_menu(self, event):
        self.context_menu.post(event.x_root, event.y_root)

    def select_all(self):
        self.text.tag_add(tk.SEL, "1.0", tk.END)
        self.text.mark_set(tk.INSERT, "1.0")
        self.text.see(tk.INSERT)

    def copy_text(self):
        selected_text = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
        pyperclip.copy(selected_text)
        
    def paste_text(self):
        clipboard_text = pyperclip.paste()
        self.text.insert(tk.INSERT, clipboard_text)
        
    def create_menu(self):
        menubar = tk.Menu(self.root)
        self.root.config(menu=menubar)

        file_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="文件", menu=file_menu)
        file_menu.add_command(label="新建", command=self.new_file)
        file_menu.add_command(label="打开", command=self.open_file)
        file_menu.add_command(label="保存", command=self.save_file)
        file_menu.add_command(label="退出", command=self.root.quit)

        edit_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="编辑", menu=edit_menu)
        edit_menu.add_command(label="加粗", command=self.make_text_bold)
        edit_menu.add_command(label="设置字体大小", command=self.set_font_size)
        
        color_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="颜色", menu=color_menu)
        color_menu.add_command(label="更改文字颜色", command=self.change_text_color)
        color_menu.add_command(label="更改背景颜色", command=self.change_background_color)
        
        about_menu = tk.Menu(menubar, tearoff=0)
        about_menu.add_command(label="版本号", command=self.show_version_info)
        about_menu.add_command(label="时间与日期", command=self.show_time_and_date)
        menubar.add_cascade(label="关于", menu=about_menu)
        
    def new_file(self):
        self.text.delete(1.0, tk.END)
        self.current_file = None

    def open_file(self):
        file_path = filedialog.askopenfilename(filetypes=[("文本文件", "*.txt")])
        if file_path:
            with open(file_path, "r") as file:
                data = json.load(file)
                content = data.get("content", "")
                styles = data.get("styles", {})
                self.text.delete(1.0, tk.END)
                self.text.insert(tk.INSERT, content)
                self.apply_styles(styles)
            self.current_file = file_path

    def save_file(self):
        if self.current_file:
            content = self.text.get(1.0, tk.END)
            styles = self.extract_styles()
            data = {"content": content, "styles": styles}
            with open(self.current_file, "w") as file:
                json.dump(data, file)
        else:
            self.save_as_file()

    def save_as_file(self):
        file_path = filedialog.asksaveasfilename(filetypes=[("文本文件", "*.txt")])
        if file_path:
            content = self.text.get(1.0, tk.END)
            styles = self.extract_styles()
            data = {"content": content, "styles": styles}
            with open(file_path, "w") as file:
                json.dump(data, file)
            self.current_file = file_path

    def make_text_bold(self):
        current_tags = self.text.tag_names("sel.first")
        if "bold" in current_tags:
            self.text.tag_remove("bold", "sel.first", "sel.last")
        else:
            self.text.tag_add("bold", "sel.first", "sel.last")
            self.text.tag_config("bold", font=("Arial", 12, "bold"))

    def set_font_size(self):
        font_size = simpledialog.askinteger("设置字体大小", "请输入字体大小:", parent=self.root, minvalue=1, maxvalue=72)
        if font_size:
            current_tags = self.text.tag_names("sel.first")
            for tag in current_tags:
                if tag.startswith("font_size_"):
                    self.text.tag_remove(tag, "sel.first", "sel.last")
            self.text.tag_add("font_size_" + str(font_size), "sel.first", "sel.last")
            self.text.tag_config("font_size_" + str(font_size), font=("Arial", font_size))

    def change_text_color(self):
        color = colorchooser.askcolor()[1]  # Ask the user to select a 
        color = colorchooser.askcolor()[1]  # 请用户选择一个颜色
        current_tags = self.text.tag_names("sel.first")
        for tag in current_tags:
            if tag.startswith("text_color_"):
                self.text.tag_remove(tag, "sel.first", "sel.last")
        self.text.tag_add("text_color_" + color, "sel.first", "sel.last")
        self.text.tag_config("text_color_" + color, foreground=color)

    def change_background_color(self):
        color = colorchooser.askcolor()[1]  # 请用户选择一个颜色
        self.text.configure(bg=color)

    def extract_styles(self):
        styles = {}
        for tag in self.text.tag_names():
            if tag.startswith("font_size_"):
                styles[tag] = self.text.tag_cget(tag, "font")
            elif tag.startswith("text_color_"):
                styles[tag] = self.text.tag_cget(tag, "foreground")
            elif tag == "bold":
                styles[tag] = True
        return styles

    def apply_styles(self, styles):
        for tag, value in styles.items():
            if tag.startswith("font_size_"):
                self.text.tag_add(tag, 1.0, tk.END)
                self.text.tag_config(tag, font=value)
            elif tag.startswith("text_color_"):
                self.text.tag_add(tag, 1.0, tk.END)
                self.text.tag_config(tag, foreground=value)
            elif tag == "bold":
                self.text.tag_add(tag, 1.0, tk.END)
                self.text.tag_config(tag, font=("Arial", 12, "bold"))

    def show_version_info(self):
        version_window = tk.Toplevel(self.root)
        version_window.title("版本信息")
        version_label = tk.Label(version_window, text="version 版本号 \n Alpha 0.19 \n B站uid:2084396804  \n 感谢使用此软件 \n testedit开发组awa")
        version_label.pack()
    
    def show_time_and_date(self):
        time_window = tk.Toplevel(self.root)
        time_window.title("时间与日期")
        current_time = time.strftime("%Y-%m-%d %H:%M:%S")
        time_label = tk.Label(time_window, text="当前时间与日期:" + current_time)
        time_label.pack()

if __name__ == "__main__":
    root = tk.Tk()
    app = TextEditor(root)
    root.mainloop()

自己去试试看,b站介绍视频详见版本号中uid

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值