python编写一个完整的中文版记事本
时间: 2024-01-04 19:04:17 浏览: 97
python实现完整记事本
以下是一个简单的中文版记事本的Python代码,它使用Tkinter库实现GUI界面:
```python
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter.filedialog import askopenfilename, asksaveasfilename
class Notepad:
def __init__(self, master):
self.master = master
self.filename = None
self.text = tk.Text(self.master, font=('Arial', 14))
self.text.pack(fill='both', expand=True)
self.create_menu()
def create_menu(self):
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_file_as)
self.file_menu.add_separator()
self.file_menu.add_command(label='退出', accelerator='Alt+F4', command=self.quit_app)
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.text.edit_undo)
self.edit_menu.add_command(label='重做', accelerator='Ctrl+Y', command=self.text.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.menu_bar.add_cascade(label='编辑', menu=self.edit_menu)
self.help_menu = tk.Menu(self.menu_bar, tearoff=0)
self.help_menu.add_command(label='关于', command=self.show_about)
self.menu_bar.add_cascade(label='帮助', menu=self.help_menu)
self.master.config(menu=self.menu_bar)
def new_file(self, event=None):
self.filename = None
self.text.delete(1.0, tk.END)
def open_file(self, event=None):
self.filename = askopenfilename(defaultextension='.txt', filetypes=[('Text Files', '*.txt'), ('All Files', '*.*')])
if self.filename:
self.text.delete(1.0, tk.END)
with open(self.filename, 'r') as f:
self.text.insert(1.0, f.read())
def save_file(self, event=None):
if self.filename:
with open(self.filename, 'w') as f:
f.write(self.text.get(1.0, tk.END))
else:
self.save_file_as()
def save_file_as(self, event=None):
self.filename = asksaveasfilename(defaultextension='.txt', filetypes=[('Text Files', '*.txt'), ('All Files', '*.*')])
if self.filename:
with open(self.filename, 'w') as f:
f.write(self.text.get(1.0, tk.END))
def quit_app(self, event=None):
if messagebox.askyesno('退出', '确定要退出吗?'):
self.master.destroy()
def cut(self, event=None):
self.text.event_generate('<<Cut>>')
def copy(self, event=None):
self.text.event_generate('<<Copy>>')
def paste(self, event=None):
self.text.event_generate('<<Paste>>')
def show_about(self, event=None):
messagebox.showinfo('关于', '这是一个简单的中文版记事本。')
if __name__ == '__main__':
root = tk.Tk()
root.title('中文版记事本')
root.geometry('800x600')
app = Notepad(root)
root.mainloop()
```
这个记事本具有基本的文本编辑功能,包括新建、打开、保存、另存为、撤销、重做、剪切、复制、粘贴等。你可以根据自己的需求对它进行扩展。
阅读全文