程序模仿实现Windows自带记事本功能:新建文件、保存、编辑、格式、帮助等功能。
时间: 2023-07-10 08:36:07 浏览: 155
模仿WINDOWS自带的记事本,所有功能都已实现!
实现Windows自带记事本的功能,需要用到一些GUI库,这里我推荐使用Python自带的GUI库tkinter来实现。
以下是一个简单的代码示例,实现了新建、保存、编辑、格式和帮助等基本功能:
```python
import tkinter as tk
from tkinter import messagebox, filedialog
class Notepad:
def __init__(self, master):
self.master = master
self.master.title("记事本")
self.textpad = tk.Text(self.master, undo=True)
self.textpad.pack(expand=True, fill="both")
self.filename = None
# 创建菜单栏
self.menu_bar = tk.Menu(self.master)
# 创建文件菜单
self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.file_menu.add_command(label="新建", accelerator="Ctrl+N", command=self.new_file)
self.file_menu.add_command(label="打开", accelerator="Ctrl+O", command=self.open_file)
self.file_menu.add_command(label="保存", accelerator="Ctrl+S", command=self.save_file)
self.file_menu.add_command(label="另存为", accelerator="Ctrl+Shift+S", command=self.save_as_file)
self.file_menu.add_separator()
self.file_menu.add_command(label="退出", command=self.exit)
self.menu_bar.add_cascade(label="文件", menu=self.file_menu)
# 创建编辑菜单
self.edit_menu = tk.Menu(self.menu_bar, tearoff=0)
self.edit_menu.add_command(label="撤销", accelerator="Ctrl+Z", command=self.textpad.edit_undo)
self.edit_menu.add_command(label="恢复", accelerator="Ctrl+Y", command=self.textpad.edit_redo)
self.edit_menu.add_separator()
self.edit_menu.add_command(label="剪切", accelerator="Ctrl+X", command=self.cut)
self.edit_menu.add_command(label="复制", accelerator="Ctrl+C", command=self.copy)
self.edit_menu.add_command(label="粘贴", accelerator="Ctrl+V", command=self.paste)
self.edit_menu.add_separator()
self.edit_menu.add_command(label="查找", accelerator="Ctrl+F", command=self.find)
self.edit_menu.add_command(label="替换", accelerator="Ctrl+H", command=self.replace)
self.menu_bar.add_cascade(label="编辑", menu=self.edit_menu)
# 创建格式菜单
self.format_menu = tk.Menu(self.menu_bar, tearoff=0)
self.format_menu.add_command(label="字体", command=self.choose_font)
self.menu_bar.add_cascade(label="格式", menu=self.format_menu)
# 创建帮助菜单
self.help_menu = tk.Menu(self.menu_bar, tearoff=0)
self.help_menu.add_command(label="关于", command=self.about)
self.menu_bar.add_cascade(label="帮助", menu=self.help_menu)
self.master.config(menu=self.menu_bar)
# 创建快捷键
self.master.bind("<Control-n>", lambda event: self.new_file())
self.master.bind("<Control-o>", lambda event: self.open_file())
self.master.bind("<Control-s>", lambda event: self.save_file())
self.master.bind("<Control-S>", lambda event: self.save_as_file())
self.master.bind("<Control-z>", lambda event: self.textpad.edit_undo())
self.master.bind("<Control-y>", lambda event: self.textpad.edit_redo())
self.master.bind("<Control-x>", lambda event: self.cut())
self.master.bind("<Control-c>", lambda event: self.copy())
self.master.bind("<Control-v>", lambda event: self.paste())
self.master.bind("<Control-f>", lambda event: self.find())
self.master.bind("<Control-h>", lambda event: self.replace())
def new_file(self):
self.textpad.delete(1.0, tk.END)
self.filename = None
def open_file(self):
self.filename = filedialog.askopenfilename(defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if self.filename:
self.textpad.delete(1.0, tk.END)
with open(self.filename, "r") as f:
self.textpad.insert(1.0, f.read())
def save_file(self):
if self.filename:
with open(self.filename, "w") as f:
f.write(self.textpad.get(1.0, tk.END))
else:
self.save_as_file()
def save_as_file(self):
self.filename = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if self.filename:
with open(self.filename, "w") as f:
f.write(self.textpad.get(1.0, tk.END))
def exit(self):
self.master.quit()
def cut(self):
self.textpad.event_generate("<<Cut>>")
def copy(self):
self.textpad.event_generate("<<Copy>>")
def paste(self):
self.textpad.event_generate("<<Paste>>")
def find(self):
find_dialog = tk.Toplevel(self.master)
find_dialog.title("查找")
find_dialog.transient(self.master)
tk.Label(find_dialog, text="查找:").grid(row=0, column=0, sticky="e")
find_entry = tk.Entry(find_dialog, width=30)
find_entry.grid(row=0, column=1, padx=2, pady=2, sticky="we")
find_entry.focus_set()
case_var = tk.IntVar()
tk.Checkbutton(find_dialog, text="区分大小写", variable=case_var).grid(row=1, column=1, sticky="e")
tk.Button(find_dialog, text="查找下一个").grid(row=0, column=2, padx=2, pady=2)
tk.Button(find_dialog, text="取消", command=find_dialog.destroy).grid(row=1, column=2, padx=2, pady=2)
def replace(self):
replace_dialog = tk.Toplevel(self.master)
replace_dialog.title("替换")
replace_dialog.transient(self.master)
tk.Label(replace_dialog, text="查找:").grid(row=0, column=0, sticky="e")
find_entry = tk.Entry(replace_dialog, width=30)
find_entry.grid(row=0, column=1, padx=2, pady=2, sticky="we")
tk.Label(replace_dialog, text="替换为:").grid(row=1, column=0, sticky="e")
replace_entry = tk.Entry(replace_dialog, width=30)
replace_entry.grid(row=1, column=1, padx=2, pady=2, sticky="we")
replace_entry.focus_set()
case_var = tk.IntVar()
tk.Checkbutton(replace_dialog, text="区分大小写", variable=case_var).grid(row=2, column=1, sticky="e")
tk.Button(replace_dialog, text="查找下一个").grid(row=0, column=2, padx=2, pady=2)
tk.Button(replace_dialog, text="替换", command=lambda: self.replace_text(find_entry.get(), replace_entry.get(), case_var.get())).grid(row=1, column=2, padx=2, pady=2)
tk.Button(replace_dialog, text="全部替换", command=lambda: self.replace_all(find_entry.get(), replace_entry.get(), case_var.get())).grid(row=2, column=2, padx=2, pady=2)
tk.Button(replace_dialog, text="取消", command=replace_dialog.destroy).grid(row=3, column=2, padx=2, pady=2)
def replace_text(self, find_str, replace_str, case_sensitive):
start_pos = 1.0
while True:
if case_sensitive:
pos = self.textpad.search(find_str, start_pos, tk.END, nocase=0)
else:
pos = self.textpad.search(find_str, start_pos, tk.END, nocase=1)
if not pos:
break
end_pos = pos + "+%dc" % len(find_str)
self.textpad.delete(pos, end_pos)
self.textpad.insert(pos, replace_str)
start_pos = pos + "+%dc" % len(replace_str)
def replace_all(self, find_str, replace_str, case_sensitive):
self.textpad.mark_set("match_start", "1.0")
self.textpad.mark_set("match_end", "1.0")
self.textpad.tag_remove("match", "1.0", tk.END)
count = tk.IntVar()
while True:
if case_sensitive:
pos = self.textpad.search(find_str, "match_end", tk.END, count=count, nocase=0)
else:
pos = self.textpad.search(find_str, "match_end", tk.END, count=count, nocase=1)
if not pos:
break
self.textpad.mark_set("match_start", pos)
end_pos = pos + "+%dc" % count.get()
self.textpad.mark_set("match_end", end_pos)
self.textpad.tag_add("match", "match_start", "match_end")
self.textpad.insert("match_end", replace_str)
self.textpad.tag_remove("match", "1.0", tk.END)
def choose_font(self):
font = tk.font.Font(font=self.textpad["font"])
font_dialog = tk.font.FontChooser(self.master, font)
new_font = font_dialog.show()
if new_font:
self.textpad["font"] = new_font
def about(self):
messagebox.showinfo("关于", "这是一个使用Python和tkinter实现的记事本程序。")
if __name__ == "__main__":
root = tk.Tk()
notepad = Notepad(root)
root.mainloop()
```
该程序具有基本的文本编辑功能,包括新建、打开、保存、另存为、撤销、恢复、剪切、复制、粘贴、查找、替换和选择字体等功能。
阅读全文