# -*- coding:utf-8 -*-
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from os.path import isfile
import pickle as pk
import easygui
# 新建文件
def new(event=None):
global title
global saved
global filename
title = "记事本 --- *New*"
saved = True
root.title(title)
filename = ""
text.delete("1.0", END)
# 键盘按下时触发
def enter(event):
global title
global saved
if event.char == "":
return False
text.edit_separator()
if saved:
list1 = list(title)
list1.insert(8, "*")
list1.extend(["*"])
title = "".join(list1)
root.title(title)
saved = False
# 打开文件
def open_file(event=None):
global filename, title
filename = easygui.fileopenbox(title="打开文件", filetypes = ["*.txt"])
if filename:
with open(filename, encoding="utf-8") as file:
text.delete("1.0", END)
text.insert("1.0", file.read())
title = "记事本 --- " + filename
root.title(title)
# 保存文件
def save(event=None):
global title, filename, saved
# 以打开文件但没有保存
if title[8] == "*" and title[-1] == "*" and title != "记事本 --- *New*":
list1 = list(title)
del list1[8]
del list1[-1]
title = "".join(list1)
root.title(title)
# 未保存文件
if title == "记事本 --- New" or title == "记事本 --- *New*":
filename = easygui.filesavebox(title="保存文件", filetypes = ["*.txt"])
if filename:
with open(filename, "w") as file:
file.write(text.get("1.0", END))
title = "记事本 --- " + filename
# 以打开文件但没有保存
else:
with open(filename, "w") as file:
file.write(text.get("1.0", END))
title = "记事本 --- " + filename
saved = True
root.title(title)
# 撤销
def undo():
global title
global saved
text.edit_undo()
if saved:
list1 = list(title)
list1.insert(8, "*")
list1.extend(["*"])
title = "".join(list1)
root.title(title)
saved = False
# 重做
def redo():
global title
global saved
text.edit_redo()
if saved:
list1 = list(title)
list1.insert(8, "*")
list1.extend(["*"])
title = "".join(list1)
root.title(title)
saved = False
# 退出
def exit_program():
if not saved:
flag = messagebox.askquestion("提示", "文件未保存,是否保存文件?")
if flag == "yes":
save()
root.quit()
root.quit()
# 确认更改字体名字
def confirm():
font_list.append(fontname.get(ACTIVE))
mst.destroy()
fontsize_dialog()
# 确认更改字体大小
def confirm2():
font_list.append(w.get())
text.config(font=tuple(font_list))
pk.dump(font_list, file=open("font.pkl", "wb"))
m.destroy()
# 调整字体
def fontname_dialog():
global mst
global fontname
font_list.clear()
mst = Toplevel(root)
mst.title("调整字体")
mst.resizable(False, False)
frame = Frame(mst)
frame.pack(side=TOP)
fontname = Listbox(frame, width=30)
fontname.pack(side=LEFT)
confirmb = Button(mst, text="确定", width=25, command=confirm)
confirmb.pack(side=BOTTOM)
for each in ["楷体", "仿宋", "黑体"]:
fontname.insert(END, each)
mst.protocol("WM_DELETE_WINDOW", set_font)
# 调整字体大小
def fontsize_dialog():
global w, m
m = Toplevel(root)
m.title("调整字体大小")
m.resizable(False, False)
w = Scale(m, from_=2, to=72, length=400, orient=HORIZONTAL, tickinterval=10)
w.pack()
confirmb = Button(m, text="确定", width=25, command=confirm2)
confirmb.pack(side=BOTTOM)
m.protocol("WM_DELETE_WINDOW", set_font)
# 获取历史字体并设置
def set_font():
fonttuple = tuple(pk.load(open("font.pkl", "rb")))
text.config(font=fonttuple)
title = "记事本 --- New"
saved = True
filename = ""
mst = None
fontname = None
w = None
m = None
if not isfile("font.pkl"):
pk.dump(("仿宋", 15), open("font.pkl", "wb"))
try:
font = pk.load(open("font.pkl", "rb"))
except EOFError:
pk.dump(("仿宋", 15), open("font.pkl", "wb"))
font = pk.load(open("font.pkl", "rb"))
font_list = list(pk.load(open("font.pkl", "rb")))
root = Tk()
root.title(title)
mb = Menu(root)
# 文件菜单
filemenu = Menu(mb, tearoff=False)
filemenu.add_command(label="新建", command=new)
filemenu.add_command(label="打开", command=open_file)
filemenu.add_command(label="保存", command=save)
filemenu.add_separator()
filemenu.add_command(label="退出", command=exit_program)
# 编辑菜单
editmenu = Menu(mb, tearoff=False)
editmenu.add_command(label="撤销", command=undo)
editmenu.add_command(label="重做", command=redo)
# 调整菜单
adjustmenu = Menu(mb, tearoff=False)
adjustmenu.add_command(label="调整字体", command=fontname_dialog)
mb.add_cascade(label="文件", menu=filemenu)
mb.add_cascade(label="编辑", menu=editmenu)
mb.add_cascade(label="调整", menu=adjustmenu)
sb = Scrollbar(root)
sb.pack(side=RIGHT, fill=Y)
text = Text(root, yscrollcommand=sb.set, font=tuple(font), undo=True, autoseparator=False, width=50, height=15)
text.pack(side=LEFT, expand=True)
sb.config(command=text.yview)
text.bind("<KeyRelease>", enter)
text.bind("<Control-s>", save)
text.bind("<Control-n>", new)
text.bind("<Control-o>", open_file)
root.config(menu=mb)
root.resizable(0, 0)
root.protocol("WM_DELETE_WINDOW", exit_program)
mainloop()