仿照windows记事本,制作一个记事本软件,实现纯文本文件的新建、打开、保存、另存、复制、粘贴、查找、替换、关于等等功能;关闭软件是弹出对话框提示是否把修改保存,根据用户选择更新磁盘文件内容。
时间: 2023-07-04 16:05:20 浏览: 118
好的,这个任务可以用Python语言和Tkinter库来实现。下面是代码实现:
```python
import tkinter as tk
from tkinter import filedialog, messagebox
class Notepad:
def __init__(self, master):
self.master = master
self.master.title("记事本——by Python")
self.master.geometry("600x400")
self.filename = ""
self.filetext = tk.Text(self.master)
self.filetext.pack(expand=True, fill=tk.BOTH)
self.create_menu()
self.master.protocol("WM_DELETE_WINDOW", self.close_app)
def create_menu(self):
menubar = tk.Menu(self.master)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="新建", accelerator="Ctrl+N", command=self.new_file)
filemenu.add_command(label="打开", accelerator="Ctrl+O", command=self.open_file)
filemenu.add_command(label="保存", accelerator="Ctrl+S", command=self.save_file)
filemenu.add_command(label="另存为", accelerator="Ctrl+Shift+S", command=self.save_as_file)
menubar.add_cascade(label="文件", menu=filemenu)
editmenu = tk.Menu(menubar, tearoff=0)
editmenu.add_command(label="复制", accelerator="Ctrl+C", command=self.copy_text)
editmenu.add_command(label="粘贴", accelerator="Ctrl+V", command=self.paste_text)
editmenu.add_command(label="查找", accelerator="Ctrl+F", command=self.find_text)
editmenu.add_command(label="替换", accelerator="Ctrl+H", command=self.replace_text)
menubar.add_cascade(label="编辑", menu=editmenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="关于", command=self.about_app)
menubar.add_cascade(label="帮助", menu=helpmenu)
self.master.config(menu=menubar)
def new_file(self, event=None):
self.filetext.delete("1.0", tk.END)
self.filename = ""
def open_file(self, event=None):
self.filename = filedialog.askopenfilename(defaultextension=".txt", filetypes=[("Text Documents", "*.txt")])
if self.filename:
with open(self.filename, "r") as f:
self.filetext.delete("1.0", tk.END)
self.filetext.insert(tk.END, f.read())
def save_file(self, event=None):
if not self.filename:
self.filename = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Documents", "*.txt")])
if self.filename:
with open(self.filename, "w") as f:
f.write(self.filetext.get("1.0", tk.END))
def save_as_file(self, event=None):
self.filename = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Documents", "*.txt")])
if self.filename:
with open(self.filename, "w") as f:
f.write(self.filetext.get("1.0", tk.END))
def copy_text(self, event=None):
self.filetext.event_generate("<<Copy>>")
def paste_text(self, event=None):
self.filetext.event_generate("<<Paste>>")
def find_text(self, event=None):
find_window = tk.Toplevel(self.master)
find_window.title("查找")
find_window.geometry("300x100")
find_label = tk.Label(find_window, text="查找内容:")
find_label.pack(side=tk.LEFT)
find_entry = tk.Entry(find_window, width=30)
find_entry.pack(side=tk.LEFT)
find_entry.focus_set()
def find():
text = find_entry.get()
if text:
start_pos = self.filetext.search(text, "1.0", stopindex=tk.END)
if start_pos:
end_pos = f"{start_pos}+{len(text)}c"
self.filetext.tag_remove(tk.SEL, "1.0", tk.END)
self.filetext.tag_add(tk.SEL, start_pos, end_pos)
self.filetext.mark_set(tk.INSERT, end_pos)
self.filetext.see(tk.INSERT)
find_window.destroy()
else:
messagebox.showinfo("查找", "未找到指定内容!")
find_button = tk.Button(find_window, text="查找", command=find)
find_button.pack(side=tk.LEFT)
def replace_text(self, event=None):
replace_window = tk.Toplevel(self.master)
replace_window.title("替换")
replace_window.geometry("300x150")
find_label = tk.Label(replace_window, text="查找内容:")
find_label.pack(side=tk.TOP)
find_entry = tk.Entry(replace_window, width=30)
find_entry.pack(side=tk.TOP)
find_entry.focus_set()
replace_label = tk.Label(replace_window, text="替换为:")
replace_label.pack(side=tk.TOP)
replace_entry = tk.Entry(replace_window, width=30)
replace_entry.pack(side=tk.TOP)
def replace():
find_text = find_entry.get()
replace_text = replace_entry.get()
if find_text and replace_text:
start_pos = self.filetext.search(find_text, "1.0", stopindex=tk.END)
if start_pos:
end_pos = f"{start_pos}+{len(find_text)}c"
self.filetext.tag_remove(tk.SEL, "1.0", tk.END)
self.filetext.tag_add(tk.SEL, start_pos, end_pos)
self.filetext.replace(tk.SEL_FIRST, tk.SEL_LAST, replace_text)
self.filetext.mark_set(tk.INSERT, start_pos)
self.filetext.see(tk.INSERT)
else:
messagebox.showinfo("替换", "未找到指定内容!")
replace_button = tk.Button(replace_window, text="替换", command=replace)
replace_button.pack(side=tk.TOP)
def about_app(self):
messagebox.showinfo("关于", "记事本——by Python")
def close_app(self):
if self.filetext.edit_modified():
answer = messagebox.askyesnocancel("退出", "是否保存文件?")
if answer:
self.save_file()
elif answer is None:
return
self.master.destroy()
if __name__ == "__main__":
root = tk.Tk()
notepad = Notepad(root)
root.mainloop()
```
这个记事本软件可以通过菜单或快捷键来操作各种功能,关闭软件时如果有修改过的内容会弹出对话框提示是否保存。还可以查找、替换文本内容。
阅读全文