|
发表于 2020-4-11 13:04:49
|
显示全部楼层
本帖最后由 zltzlt 于 2020-4-11 13:06 编辑
好,我给你一段之前写的代码
- # -*- coding:utf-8 -*-
- from tkinter import *
- from tkinter import filedialog
- from tkinter import messagebox
- from os.path import isfile
- from pynput.keyboard import Key, Controller
- import pickle as pk
- def new(event=None):
- global title
- global saved
- global filename
- title = "记事本 --- 未命名"
- saved = True
- root.title(title)
- filename = ''
- text.delete("1.0", END)
- def enter(event):
- global title
- global saved
- 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
- global title
- filename = filedialog.askopenfilename(title="打开文件",
- defaultextension=".txt",
- filetypes=[("Text File", ".txt"),
- ("All File", ".*")])
- 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
- global filename
- global saved
- if ((title[8] == "*" and title[-1] == "*") and
- title != "记事本 --- *未命名*"):
- list1 = list(title)
- del list1[8]
- del list1[-1]
- title = "".join(list1)
- root.title(title)
- if title == "记事本 --- 未命名" or title == "记事本 --- *未命名*":
- file_name = filedialog.asksaveasfilename(
- title="保存文件",
- defaultextension=".py",
- filetypes=[("Text File", ".txt"), ("All File", ".*")])
- if file_name:
- with open(file_name, "w") as file:
- file.write(text.get("1.0", END))
- title = "记事本 --- " + file_name
- else:
- with open(filename, "w") as file:
- file.write(text.get("1.0", END))
- title = "记事本 --- " + filename
- saved = True
- root.title(title)
- def undo(event=None):
- 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(event=None):
- 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(event=None):
- if not saved:
- ok = messagebox.askquestion("询问", "您的文件还没有保存!是否保存文件后再退出?")
- if ok == "yes":
- save()
- root.quit()
- ok = messagebox.askquestion("询问", "您确定要退出吗?")
- if ok == "yes":
- 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(event=None):
- 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
- global 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)
- def copy(event=None):
- with kb.pressed(Key.ctrl):
- kb.press('c')
- kb.release('c')
- def paste(event=None):
- with kb.pressed(Key.ctrl):
- kb.press('v')
- kb.release('v')
- def cut(event=None):
- with kb.pressed(Key.ctrl):
- kb.press('x')
- kb.release('x')
- title = "记事本 --- 未命名"
- saved = True
- filename = ''
- mst = None
- fontname = None
- w = None
- m = None
- kb = Controller()
- 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="新建 Ctrl+N", command=new)
- filemenu.add_command(label="打开 Ctrl+O", command=open_file)
- filemenu.add_command(label="保存 Ctrl+S", command=save)
- filemenu.add_separator()
- filemenu.add_command(label="退出 Ctrl+Q", command=exit_program)
- mb.add_cascade(label="文件 (F)", menu=filemenu)
- editmenu = Menu(mb, tearoff=False)
- editmenu.add_command(label="剪切 Ctrl+X", command=cut)
- editmenu.add_command(label="复制 Ctrl+C", command=copy)
- editmenu.add_command(label="粘贴 Ctrl+V", command=paste)
- editmenu.add_command(label="撤销 Ctrl+Z", command=undo)
- editmenu.add_command(label="重做 Ctrl+Y", command=redo)
- mb.add_cascade(label="编辑 (E)", menu=editmenu)
- adjustmenu = Menu(mb, tearoff=False)
- adjustmenu.add_command(label="调整字体 Ctrl+F", command=fontname_dialog)
- mb.add_cascade(label="调整 (A)", 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)
- root.config(menu=mb)
- # 不可调整窗口大小
- root.resizable(False, False)
- root.protocol("WM_DELETE_WINDOW", exit_program)
- text.bind("<Control-KeyPress-n>", new)
- text.bind("<Control-KeyPress-o>", open_file)
- text.bind("<Control-KeyPress-s>", save)
- text.bind("<Control-KeyPress-q>", exit_program)
- text.bind("<Control-KeyPress-x>", cut)
- text.bind("<Control-KeyPress-c>", copy)
- text.bind("<Control-KeyPress-v>", paste)
- text.bind("<Control-KeyPress-z>", undo)
- text.bind("<Control-KeyPress-y>", redo)
- text.bind("<Control-KeyPress-f>", fontname_dialog)
- mainloop()
复制代码 |
|