python实现可以编辑文字样式、可以加粗、下划线文字,可以引入图片的记事本源码
时间: 2023-11-28 15:04:53 浏览: 82
以下是一个简单的Python Tkinter GUI程序,实现了编辑文字样式、加粗、下划线文字和引入图片的记事本功能。你可以根据自己的需求进行修改和扩展。
```
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import font
from PIL import Image, ImageTk
class Notepad:
def __init__(self, master):
self.master = master
self.master.title("Notepad")
self.text = tk.Text(self.master, font=("Helvetica", 12))
self.text.pack(fill="both", expand=True)
self.bold_button = ttk.Button(self.master, text="B", command=self.bold_text)
self.bold_button.pack(side="left", padx=5, pady=5)
self.underline_button = ttk.Button(self.master, text="U", command=self.underline_text)
self.underline_button.pack(side="left", padx=5, pady=5)
self.image_button = ttk.Button(self.master, text="Insert Image", command=self.insert_image)
self.image_button.pack(side="left", padx=5, pady=5)
self.menu_bar = tk.Menu(self.master)
self.master.config(menu=self.menu_bar)
self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.file_menu.add_command(label="New", command=self.new_file)
self.file_menu.add_command(label="Open", command=self.open_file)
self.file_menu.add_command(label="Save", command=self.save_file)
self.file_menu.add_command(label="Save As", command=self.save_file_as)
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.master.quit)
self.menu_bar.add_cascade(label="File", menu=self.file_menu)
self.edit_menu = tk.Menu(self.menu_bar, tearoff=0)
self.edit_menu.add_command(label="Cut", command=self.cut_text)
self.edit_menu.add_command(label="Copy", command=self.copy_text)
self.edit_menu.add_command(label="Paste", command=self.paste_text)
self.menu_bar.add_cascade(label="Edit", menu=self.edit_menu)
self.master.bind("<Control-n>", self.new_file)
self.master.bind("<Control-o>", self.open_file)
self.master.bind("<Control-s>", self.save_file)
def new_file(self, event=None):
self.master.title("Untitled")
self.text.delete(1.0, tk.END)
def open_file(self, event=None):
file_path = filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
self.master.title(file_path)
self.text.delete(1.0, tk.END)
with open(file_path, "r") as file:
self.text.insert(tk.END, file.read())
def save_file(self, event=None):
if self.master.title() == "Untitled":
self.save_file_as()
else:
with open(self.master.title(), "w") as file:
file.write(self.text.get(1.0, tk.END))
def save_file_as(self, event=None):
file_path = filedialog.asksaveasfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
self.master.title(file_path)
with open(file_path, "w") as file:
file.write(self.text.get(1.0, tk.END))
def cut_text(self, event=None):
self.text.event_generate("<<Cut>>")
def copy_text(self, event=None):
self.text.event_generate("<<Copy>>")
def paste_text(self, event=None):
self.text.event_generate("<<Paste>>")
def bold_text(self):
bold_font = font.Font(self.text, self.text.cget("font"))
bold_font.config(weight="bold")
self.text.tag_configure("bold", font=bold_font)
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")
def underline_text(self):
underline_font = font.Font(self.text, self.text.cget("font"))
underline_font.config(underline=True)
self.text.tag_configure("underline", font=underline_font)
current_tags = self.text.tag_names("sel.first")
if "underline" in current_tags:
self.text.tag_remove("underline", "sel.first", "sel.last")
else:
self.text.tag_add("underline", "sel.first", "sel.last")
def insert_image(self):
file_path = filedialog.askopenfilename(defaultextension=".jpg",
filetypes=[("JPEG Files", "*.jpg"), ("All Files", "*.*")])
if file_path:
image = Image.open(file_path)
photo = ImageTk.PhotoImage(image)
self.text.image_create(tk.INSERT, image=photo)
self.text.image = photo
if __name__ == "__main__":
root = tk.Tk()
notepad = Notepad(root)
root.mainloop()
```
注意:在运行程序之前,需要安装Pillow库(`pip install Pillow`),以便能够处理和显示图像。
阅读全文