python实现记事本可以保存可以编辑字体、编码源码
时间: 2023-11-13 15:04:39 浏览: 141
可以的,你可以使用Python的Tkinter库来实现一个简单的记事本应用程序,同时使用Python内置的文件操作函数来实现保存和读取功能。
以下是一个简单的实现代码,可以保存、打开和编辑文本文件,并且可以选择字体和编码方式:
```python
import tkinter as tk
from tkinter import filedialog, messagebox
class Notepad:
def __init__(self, master):
self.master = master
self.master.title("记事本")
self.filename = None
self.encoding = "utf-8"
# 创建菜单栏
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)
self.file_menu.add_command(label="另存为", accelerator="Ctrl+Shift+S", command=self.save_as)
self.file_menu.add_separator()
self.file_menu.add_command(label="退出", accelerator="Ctrl+Q", 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+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.menu_bar.add_cascade(label="编辑", menu=self.edit_menu)
self.view_menu = tk.Menu(self.menu_bar, tearoff=0)
self.font_menu = tk.Menu(self.view_menu, tearoff=0)
self.font_menu.add_command(label="微软雅黑", command=lambda: self.set_font("微软雅黑"))
self.font_menu.add_command(label="宋体", command=lambda: self.set_font("宋体"))
self.font_menu.add_command(label="楷体", command=lambda: self.set_font("楷体"))
self.view_menu.add_cascade(label="字体", menu=self.font_menu)
self.encoding_menu = tk.Menu(self.view_menu, tearoff=0)
self.encoding_menu.add_command(label="UTF-8", command=lambda: self.set_encoding("utf-8"))
self.encoding_menu.add_command(label="GBK", command=lambda: self.set_encoding("gbk"))
self.view_menu.add_cascade(label="编码", menu=self.encoding_menu)
self.menu_bar.add_cascade(label="查看", menu=self.view_menu)
self.master.config(menu=self.menu_bar)
# 创建文本框
self.text = tk.Text(self.master, wrap="word")
self.text.pack(expand="yes", fill="both")
# 创建快捷键
self.master.bind("<Control-N>", lambda event: self.new_file())
self.master.bind("<Control-n>", lambda event: self.new_file())
self.master.bind("<Control-O>", lambda event: self.open_file())
self.master.bind("<Control-o>", lambda event: self.open_file())
self.master.bind("<Control-S>", lambda event: self.save())
self.master.bind("<Control-s>", lambda event: self.save())
self.master.bind("<Control-Shift-S>", lambda event: self.save_as())
self.master.bind("<Control-q>", lambda event: self.exit())
self.master.bind("<Control-Q>", lambda event: self.exit())
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())
def new_file(self):
self.filename = None
self.text.delete("1.0", "end")
def open_file(self):
filetypes = (("Text files", "*.txt"), ("All files", "*.*"))
filename = filedialog.askopenfilename(filetypes=filetypes)
if filename:
self.filename = filename
with open(filename, encoding=self.encoding) as f:
self.text.delete("1.0", "end")
self.text.insert("1.0", f.read())
def save(self):
if not self.filename:
self.save_as()
else:
with open(self.filename, "w", encoding=self.encoding) as f:
f.write(self.text.get("1.0", "end"))
def save_as(self):
filetypes = (("Text files", "*.txt"), ("All files", "*.*"))
filename = filedialog.asksaveasfilename(filetypes=filetypes)
if filename:
self.filename = filename
self.save()
def exit(self):
if messagebox.askokcancel("退出", "确定要退出吗?"):
self.master.destroy()
def cut(self):
self.text.event_generate("<<Cut>>")
def copy(self):
self.text.event_generate("<<Copy>>")
def paste(self):
self.text.event_generate("<<Paste>>")
def set_font(self, font):
self.text.config(font=(font, 14))
def set_encoding(self, encoding):
self.encoding = encoding
if self.filename:
with open(self.filename, encoding=self.encoding) as f:
self.text.delete("1.0", "end")
self.text.insert("1.0", f.read())
root = tk.Tk()
notepad = Notepad(root)
root.mainloop()
```
在这个记事本应用程序中,你可以使用菜单栏或快捷键来打开、保存、另存为、剪切、复制、粘贴、设置字体和编码方式。你也可以直接在文本框中编辑文本,所有的更改都会自动保存。
阅读全文