找回密码
 中文实名注册
查看: 69|回复: 0

文本编辑器 turtle 温暖 传真

[复制链接]

9

主题

29

帖子

1597

积分

金牌会员

Rank: 6Rank: 6

积分
1597
发表于 2025-2-8 10:57:41 | 显示全部楼层 |阅读模式

import tkinter as tk
from tkinter import colorchooser, simpledialog
import tkinter.font as font

# 封面窗口类
class CoverWindow(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("封面")
        self.geometry("400x300")
        label = tk.Label(self, text="欢迎使用文本编辑器", font=("Arial", 20))
        label.pack(pady=100)
        # 点击按钮关闭封面并显示编辑器
        button = tk.Button(self, text="开始编辑", command=self.close_cover)
        button.pack()

    def close_cover(self):
        self.destroy()

# 主编辑器窗口类
class TextEditor(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("文本编辑器")
        self.geometry("1080x900")
        # 显示封面
        self.cover = CoverWindow(self)
        self.wait_window(self.cover)
        # 创建文本输入框
        self.text = tk.Text(self)
        self.text.pack(fill=tk.BOTH, expand=True)
        # 绑定 Esc 键显示设置菜单
        self.bind("<Escape>", self.show_settings)
        # 初始化字体样式
        self.current_font = font.Font(font=self.text['font'])
        self.current_color = "black"

    def show_settings(self, event=None):
        # 创建设置窗口
        settings_window = tk.Toplevel(self)
        settings_window.title("设置")
        # 存档按钮
        save_button = tk.Button(settings_window, text="存档", command=self.save_file)
        save_button.pack(pady=10)
        # 调整字体颜色按钮
        color_button = tk.Button(settings_window, text="调整字体颜色", command=self.change_text_color)
        color_button.pack(pady=10)
        # 调整字体样式按钮
        style_button = tk.Button(settings_window, text="调整字体样式", command=self.change_text_style)
        style_button.pack(pady=10)

    def save_file(self):
        # 获取用户输入的文件名
        file_name = simpledialog.askstring("存档", "请输入文件名:")
        if file_name:
            if not file_name.endswith(".txt"):
                file_name += ".txt"
            # 获取文本框中的内容
            content = self.text.get("1.0", tk.END)
            try:
                # 保存文件
                with open(file_name, "w", encoding="utf-8") as file:
                    file.write(content)
            except Exception as e:
                print(f"保存文件时出错:{e}")

    def change_text_color(self):
        # 选择颜色
        color = colorchooser.askcolor()[1
        if color:
            self.current_color = color
            self.text.config(fg=color)

    def change_text_style(self):
        # 获取用户输入的字体样式
        font_family = simpledialog.askstring("调整字体样式", "请输入字体名称(如 Arial):")
        if font_family:
            size = simpledialog.askinteger("调整字体样式", "请输入字体大小:", initialvalue=self.current_font.cget("size"))
            if size:
                # 更新字体样式
                self.current_font.configure(family=font_family, size=size)
                self.text.config(font=self.current_font)

if __name__ == "__main__":
    editor = TextEditor()
    editor.mainloop()


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2025-3-12 12:34 , Processed in 0.037816 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表