2. 参考记事本,用Python实现一个简易记事本,记事本具有如下功能: 1)有主菜单和弹出菜单,主菜单有子菜单和单选或多选菜单 2)在记事本中能新建、存储、编辑、修改文本 3)能修改文本的前景色、背景色 4)能改变文本的字号大小 用python
时间: 2024-03-09 16:45:36 浏览: 20
好的,以下是一个简单的记事本实现,具有主菜单和弹出菜单,可以进行新建、存储、编辑、修改文本等功能:
```python
from tkinter import *
from tkinter import filedialog
class Notepad:
current_open_file = "no_file"
def __init__(self, master):
self.master = master
master.title("记事本")
self.textarea = Text(master, undo=True)
self.textarea.pack(expand=YES, fill=BOTH)
self.menubar = Menu(master)
self.filemenu = Menu(self.menubar, tearoff=0)
self.filemenu.add_command(label="新建", accelerator="Ctrl+N", command=self.new_file)
self.filemenu.add_command(label="打开", accelerator="Ctrl+O", command=self.open_file)
self.filemenu.add_command(label="保存", accelerator="Ctrl+S", command=self.save_file)
self.filemenu.add_separator()
self.filemenu.add_command(label="退出", accelerator="Alt+F4", command=master.quit)
self.menubar.add_cascade(label="文件", menu=self.filemenu)
self.editmenu = Menu(self.menubar, tearoff=0)
self.editmenu.add_command(label="撤销", accelerator="Ctrl+Z", command=self.textarea.edit_undo)
self.editmenu.add_command(label="重做", accelerator="Ctrl+Y", command=self.textarea.edit_redo)
self.editmenu.add_separator()
self.editmenu.add_command(label="剪切", accelerator="Ctrl+X", command=lambda: self.textarea.event_generate("<Control x>"))
self.editmenu.add_command(label="复制", accelerator="Ctrl+C", command=lambda: self.textarea.event_generate("<Control c>"))
self.editmenu.add_command(label="粘贴", accelerator="Ctrl+V", command=lambda: self.textarea.event_generate("<Control v>"))
self.editmenu.add_separator()
self.editmenu.add_command(label="全选", accelerator="Ctrl+A", command=lambda: self.textarea.event_generate("<Control a>"))
self.menubar.add_cascade(label="编辑", menu=self.editmenu)
self.formatmenu = Menu(self.menubar, tearoff=0)
self.formatmenu.add_command(label="字体大小", command=self.font_size)
self.formatmenu.add_command(label="前景色", command=self.foreground_color)
self.formatmenu.add_command(label="背景色", command=self.background_color)
self.menubar.add_cascade(label="格式", menu=self.formatmenu)
master.config(menu=self.menubar)
self.bind_shortcuts()
def bind_shortcuts(self):
self.textarea.bind("<Control-n>", self.new_file)
self.textarea.bind("<Control-o>", self.open_file)
self.textarea.bind("<Control-s>", self.save_file)
def new_file(self, event=None):
self.textarea.delete(1.0, END)
self.current_open_file = "no_file"
def open_file(self, event=None):
file_path = filedialog.askopenfilename()
if file_path:
self.current_open_file = file_path
self.textarea.delete(1.0, END)
with open(file_path, "r") as f:
self.textarea.insert(1.0, f.read())
def save_file(self, event=None):
if self.current_open_file == "no_file":
file_path = filedialog.asksaveasfilename()
if file_path:
self.current_open_file = file_path
with open(file_path, "w") as f:
f.write(self.textarea.get(1.0, END))
else:
with open(self.current_open_file, "w") as f:
f.write(self.textarea.get(1.0, END))
def font_size(self):
size = simpledialog.askinteger("字体大小", "请输入字体大小", initialvalue=10)
self.textarea.configure(font=("Arial", size))
def foreground_color(self):
color = colorchooser.askcolor(title="前景色")
self.textarea.configure(foreground=color[1])
def background_color(self):
color = colorchooser.askcolor(title="背景色")
self.textarea.configure(background=color[1])
root = Tk()
notepad = Notepad(root)
root.mainloop()
```
注意,该记事本仅为简单实现,可能存在功能不完善或有待改进的地方。