python实现可以保存、可以加粗、下划线文字、可以更改编码、可以更改字体、字号的记事本源码
时间: 2023-08-01 15:12:15 浏览: 116
以下是一个简单的Python记事本程序的源代码,支持保存、加粗、下划线、更改编码、更改字体和字号功能。
```python
import tkinter as tk
import tkinter.filedialog as filedialog
import tkinter.messagebox as messagebox
import tkinter.font as font
class TextEditor:
def __init__(self, master):
self.master = master
self.filename = None
self.bold = False
self.underline = False
self.encoding = 'utf-8'
self.font_family = 'Arial'
self.font_size = 12
# 创建菜单
self.menu = tk.Menu(master)
self.file_menu = tk.Menu(self.menu, tearoff=False)
self.edit_menu = tk.Menu(self.menu, tearoff=False)
self.font_menu = tk.Menu(self.menu, tearoff=False)
self.menu.add_cascade(label='File', menu=self.file_menu)
self.menu.add_cascade(label='Edit', menu=self.edit_menu)
self.menu.add_cascade(label='Font', menu=self.font_menu)
self.master.config(menu=self.menu)
# 添加文件菜单项
self.file_menu.add_command(label='New', command=self.new_file)
self.file_menu.add_command(label='Open', command=self.open_file)
self.file_menu.add_command(label='Save', command=self.save_file)
self.file_menu.add_command(label='Save As', command=self.save_file_as)
self.file_menu.add_separator()
self.file_menu.add_command(label='Exit', command=self.exit)
# 添加编辑菜单项
self.edit_menu.add_command(label='Bold', command=self.toggle_bold)
self.edit_menu.add_command(label='Underline', command=self.toggle_underline)
# 添加字体菜单项
self.font_menu.add_command(label='Font Family', command=self.choose_font_family)
self.font_menu.add_command(label='Font Size', command=self.choose_font_size)
# 创建文本框
self.text = tk.Text(master, font=(self.font_family, self.font_size), wrap='word')
self.text.pack(expand=True, fill='both')
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(title='Open', filetypes=filetypes)
if filename:
self.filename = filename
with open(filename, 'r', encoding=self.encoding) as f:
self.text.delete('1.0', 'end')
self.text.insert('1.0', f.read())
def save_file(self):
if self.filename:
with open(self.filename, 'w', encoding=self.encoding) as f:
f.write(self.text.get('1.0', 'end'))
else:
self.save_file_as()
def save_file_as(self):
filetypes = [('Text Files', '*.txt'), ('All Files', '*.*')]
filename = filedialog.asksaveasfilename(title='Save As', filetypes=filetypes)
if filename:
self.filename = filename
with open(filename, 'w', encoding=self.encoding) as f:
f.write(self.text.get('1.0', 'end'))
def exit(self):
self.master.destroy()
def toggle_bold(self):
self.bold = not self.bold
self.update_font()
def toggle_underline(self):
self.underline = not self.underline
self.update_font()
def choose_font_family(self):
font_family = font.Font(family=self.font_family, size=self.font_size).family
self.font_family = filedialog.askstring('Font Family', 'Enter a font family:', initialvalue=font_family)
self.update_font()
def choose_font_size(self):
self.font_size = filedialog.askinteger('Font Size', 'Enter a font size:', initialvalue=self.font_size)
self.update_font()
def update_font(self):
font_weight = 'bold' if self.bold else 'normal'
font_underline = 1 if self.underline else 0
self.text.configure(font=(self.font_family, self.font_size, font_weight, font_underline))
if __name__ == '__main__':
root = tk.Tk()
root.title('Text Editor')
editor = TextEditor(root)
root.mainloop()
```
该程序使用Tkinter库构建GUI界面,创建菜单和文本框,并实现了保存、打开、保存为、加粗、下划线、更改编码、更改字体和字号等功能。使用该程序时,用户可以通过菜单项或快捷键来调用这些功能。
阅读全文