简易文本编辑器的实现:
import tkinter import tkinter.colorchooser import tkinter.messagebox import tkinter.scrolledtext import tkinter.simpledialog import tkinter.filedialog #创建应用程序窗口 app = tkinter.Tk() app.title("My Notepad") app["width"] = 800 app["height"] = 600 #标记当前内容是否发生过改变,是否需要保存 textChanged = tkinter.IntVar(app, value=0) #当前文件名 filename = "" #创建菜单 menu = tkinter.Menu(app) #File子菜单 #tearoff=0表示该子菜单不可以独立 #tearoff=1时,子菜单顶端会有一个虚线 submenu = tkinter.Menu(menu, tearoff=0) def Open(): global filename #如果内容已改变,先保存 if textChanged.get(): yesno = tkinter.messagebox.askyesno(title="Save or not?", message="Do you want to save?") if yesno == tkinter.YES: Save() filename = tkinter.filedialog.askopenfilename(title="Open file", filetypes=[("Text files", "*.txt")]) if filename: txtContent.delete(0.0, tkinter.END) with open(filename, "r") as fp: txtContent.insert(tkinter.INSERT, "".join(fp.readlines())) textChanged.set(0) #创建Open菜单并绑定菜单时间处理函数 submenu.add_command(label="Open", command=Open) def Save(): global filename #如果是第一次保存新建文件,则打开“另存为”窗口 if not filename: SaveAs() elif textChanged.get(): with open(filename, "w") as fp: fp.write(txtContent.get(0.0, tkinter.END)) textChanged.set(0) #创建Save菜单并绑定菜单时间处理函数 submenu.add_command(label="Save", command=Save) def SaveAs(): global filename #打开“另存为”窗口 newfilename = tkinter.filedialog.asksaveasfilename(title="Save As", initialdir=r"c:\", initialfile="new.txt") #如果指定了文件名,则保存文件 if newfilename: with open(newfilename, "w") as fp: fp.write(txtContent.get(0.0, tkinter.END)) filename = newfilename textChanged.set(0) submenu.add_command(label="Save as", command=SaveAs) #添加分割线 submenu.add_separator() def Close(): global filename Save() txtContent.delete(0.0, tkinter.END) #置空文件名 filename = "" submenu.add_command(label="Close", command=Close) #将子菜单关联到主菜单 menu.add_cascade(label="File", menu=submenu) #Edit子菜单 submenu = tkinter.Menu(menu, tearoff=0) #撤销最后一次操作 def Undo(): txtContent["Undo"] = True try: txtContent.edit_undo() except Exception as e: pass submenu.add_command(label="Undo", command=Undo) def Redo(): txtContent["Redo"] = True try: txtContent.edit_redo() except Exception as e: pass submenu.add_command(label="Redo", command=Redo) submenu.add_separator() def Copy(): txtContent.cilpboard_clear() txtContent.clipboard_append(txtContent.selection_get()) submenu.add_command(label="Copy", command=Copy) def Cut(): Copy() txtContent.delete(tkinter.SEL_FIRST, tkinter.SEL_LAST) submenu.add_command(label="Cut", command=Cut) def Paste(): try: txtContent.insert(tkinter.SEL_FIRST, txtContent.clipboard_get()) txtContent.delete(tkinter.SEL_FIRST, tkinter.SEL_LAST) return except Exception as e: pass txtContent.insert(tkinter.INSERT, txtContent.clipboard_get()) submenu.add_command(label="Paste", command=Paste) submenu.add_separator() def Search(): textToSearch = tkinter.simpledialog.askstring(title="Search", prompt="What to search?") start = txtContent.search(textToSearch, 0.0, tkinter.END) if start: tkinter.messagebox.showinfo(title="Found", message="Ok") else: tkinter.messagebox.showinfo(title="Not found", message="Fail") submenu.add_command(label="Search", command=Search) menu.add_cascade(label="Edit", menu=submenu) #Help菜单 submenu = tkinter.Menu(menu, tearoff=0) def About(): tkinter.messagebox.showinfo(title="About", message="writer: HighLights") submenu.add_command(label="About", command=About) menu.add_cascade(label="Help", menu=submenu) #将创建的菜单关联到应用程序窗口 app.config(menu=menu) #创建文本编辑组件 txtContent = tkinter.scrolledtext.ScrolledText(app, wrap=tkinter.WORD) txtContent.pack(fill=tkinter.BOTH, expand=tkinter.YES) def KeyPress(event): textChanged.set(1) txtContent.bind("<KeyPress>", KeyPress) app.mainloop()