Python(红色的高级文本文档)
酸乳甜梅
2025年05月19日 20:44

#!/usr/bin/python3

import tkinter as tk

from tkinter import ttk, scrolledtext, filedialog, messagebox

from math import sin, cos, radians

def new_file():

result_text.delete(1.0, tk.END)

root.title("未命名 - 文本文档")

current_file = None

def open_file():

file_path = filedialog.askopenfilename(filetypes=[("文本文件", "*.txt")])

if file_path:

try:

with open(file_path, 'r', encoding='utf-8') as f:

content = f.read()

result_text.delete(1.0, tk.END)

result_text.insert(tk.END, content)

root.title(f"{file_path} - 文本文档")

current_file = file_path

except Exception as e:

messagebox.showerror("错误", str(e))

def save_file():

global current_file

if not current_file:

save_as_file()

else:

try:

content = result_text.get(1.0, tk.END)

with open(current_file, 'w', encoding='utf-8') as f:

f.write(content)

root.title(f"{current_file} - 文本文档")

except Exception as e:

messagebox.showerror("错误", str(e))

def save_as_file():

global current_file

file_path = filedialog.asksaveasfilename(

defaultextension=".txt",

filetypes=[("文本文件", "*.txt"), ("所有文件", "*.*")]

)

if file_path:

try:

content = result_text.get(1.0, tk.END)

with open(file_path, 'w', encoding='utf-8') as f:

f.write(content)

root.title(f"{file_path} - 文本文档")

current_file = file_path

except Exception as e:

messagebox.showerror("错误", str(e))

def copy_text():

try:

selected_text = result_text.selection_get()

root.clipboard_clear()

root.clipboard_append(selected_text)

except tk.TclError:

pass

def paste_text():

try:

clipboard_text = root.clipboard_get()

result_text.insert(tk.INSERT, clipboard_text)

except tk.TclError:

pass

def cut_text():

copy_text()

result_text.delete('sel.first', 'sel.last')

current_file = None

root = tk.Tk()

root.title("未命名 - 文本文档")

root.geometry("1600x800")

root.resizable(True, True)

# 创建一个Canvas用于绘制五角星

canvas = tk.Canvas(root, bg='red')

canvas.pack(fill=tk.BOTH, expand=True)

# 计算五角星的顶点坐标,以窗口中心为基准

center_x = root.winfo_width() // 2

center_y = root.winfo_height() // 2

radius = min(root.winfo_width(), root.winfo_height()) // 4

points = []

for i in range(5):

angle = radians(162 + i * 72)

x = center_x + radius * cos(angle)

y = center_y + radius * sin(angle)

points.extend([x, y])

angle = radians(234 + i * 72)

x = center_x + radius * cos(angle)

y = center_y + radius * sin(angle)

points.extend([x, y])

# 绘制黄色五角星

canvas.create_polygon(points, fill='yellow', outline='black')

# 菜单栏

menu_bar = tk.Menu(root, bg='darkred', fg='white')

file_menu = tk.Menu(menu_bar, tearoff=0, bg='darkred', fg='white')

file_menu.add_command(label="新建", accelerator="Ctrl+N", command=new_file)

file_menu.add_command(label="打开", accelerator="Ctrl+O", command=open_file)

file_menu.add_command(label="保存", accelerator="Ctrl+S", command=save_file)

file_menu.add_command(label="另存为", command=save_as_file)

file_menu.add_separator()

file_menu.add_command(label="退出", command=root.quit)

menu_bar.add_cascade(label="文件", menu=file_menu)

edit_menu = tk.Menu(menu_bar, tearoff=0, bg='darkred', fg='white')

edit_menu.add_command(label="复制", accelerator="Ctrl+C", command=copy_text)

edit_menu.add_command(label="粘贴", accelerator="Ctrl+V", command=paste_text)

edit_menu.add_command(label="剪切", accelerator="Ctrl+X", command=cut_text)

menu_bar.add_cascade(label="编辑", menu=edit_menu)

root.config(menu=menu_bar)

# 文本区域

result_text = scrolledtext.ScrolledText(

canvas,

wrap=tk.WORD,

undo=True,

bg='red',

fg='white',

insertbackground='white' # 插入光标颜色

)

result_text.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

# 绑定快捷键

root.bind('<Control-n>', lambda e: new_file())

root.bind('<Control-o>', lambda e: open_file())

root.bind('<Control-s>', lambda e: save_file())

root.bind('<Control-c>', lambda e: copy_text())

root.bind('<Control-v>', lambda e: paste_text())

root.bind('<Control-x>', lambda e: cut_text())

root.mainloop()