这是一个简单的记事本,使用Python编写。
在网上也有很多的相关代码,发现不是这里有问题,就是那里有问题,要么就是功能太简单。
这个是我参考网上相关代码,增删debug后的一个可用版本,仅供参考。
下面是一个效果图
下面是源代码
1 # -*- coding: utf-8 -*- 2 # @Time : 2019/5/1 3 # @Author : water66 4 # @Site : 5 # @File : notepad.py 6 # @Version : 1.0 7 # @Python Version : 3.6 8 # @Software: PyCharm 9 10 11 from tkinter import * 12 from tkinter.filedialog import * 13 from tkinter.messagebox import * 14 from tkinter import scrolledtext 15 import os 16 17 filename='' 18 19 20 def author(): 21 showinfo(title="作者", message="water66") 22 23 24 def power(): 25 showinfo(title="版权信息", message="water66") 26 27 28 def new_file(*args): 29 global top, filename, textPad 30 top.title("未命名文件") 31 filename = None 32 textPad.delete(1.0, END) 33 34 35 def open_file(*args): 36 global filename 37 filename = askopenfilename(defaultextension=".txt") 38 if filename == "": 39 filename = None 40 else: 41 top.title(""+os.path.basename(filename)) 42 textPad.delete(1.0, END) 43 f = open(filename, 'r', encoding="utf-8") 44 textPad.insert(1.0, f.read()) 45 f.close() 46 47 48 def click_open(event): 49 global filename 50 top.title("" + os.path.basename(filename)) 51 textPad.delete(1.0, END) 52 f = open(filename, 'r', encoding="utf-8") 53 textPad.insert(1.0, f.read()) 54 f.close() 55 56 57 def save(*args): 58 global filename 59 try: 60 f=open(filename, 'w', encoding="utf-8") 61 msg=textPad.get(1.0, 'end') 62 f.write(msg) 63 f.close() 64 except: 65 save_as() 66 67 68 def save_as(*args): 69 global filename 70 f = asksaveasfilename(initialfile="未命名.txt", defaultextension=".txt") 71 filename = f 72 fh = open(f, 'w', encoding="utf-8") 73 msg = textPad.get(1.0, END) 74 fh.write(msg) 75 fh.close() 76 top.title(""+os.path.basename(f)) 77 78 79 def rename(newname): 80 global filename 81 name = os.path.basename(os.path.splitext(filename)[0]) 82 oldpath = filename 83 newpath = os.path.dirname(oldpath)+'/'+newname+'.txt' 84 os.rename(oldpath, newpath) 85 filename = newpath 86 refresh() 87 88 89 def rename_file(*args): 90 global filename 91 t = Toplevel() 92 t.geometry("260x80+200+250") 93 t.title('重命名') 94 frame = Frame(t) 95 frame.pack(fill=X) 96 lable = Label(frame, text="文件名") 97 lable.pack(side=LEFT, padx=5) 98 var = StringVar() 99 e1 = Entry(frame, textvariable=var) 100 e1.pack(expand=YES, fill=X, side=RIGHT) 101 botton = Button(t, text="确定", command=lambda: rename(var.get())) 102 botton.pack(side=BOTTOM, pady=10) 103 104 105 def delete(*args): 106 global filename, top 107 choice = askokcancel('提示', '要执行此操作吗') 108 if choice: 109 if os.path.exists(filename): 110 os.remove(filename) 111 textPad.delete(1.0, END) 112 top.title("记事本") 113 filename = '' 114 115 116 def cut(): 117 global textPad 118 textPad.event_generate("<<Cut>>") 119 120 121 def copy(): 122 global textPad 123 textPad.event_generate("<<Copy>>") 124 125 126 def paste(): 127 global textPad 128 textPad.event_generate("<<Paste>>") 129 130 131 def undo(): 132 global textPad 133 textPad.event_generate("<<Undo>>") 134 135 136 def redo(): 137 global textPad 138 textPad.event_generate("<<Redo>>") 139 140 141 def select_all(): 142 global textPad 143 textPad.tag_add("sel", "1.0", "end") 144 145 146 def find(*agrs): 147 global textPad 148 t = Toplevel(top) 149 t.title("查找") 150 t.geometry("260x60+200+250") 151 t.transient(top) 152 Label(t, text="查找:").grid(row=0, column=0, sticky="e") 153 v = StringVar() 154 e = Entry(t, width=20, textvariable=v) 155 e.grid(row=0, column=1, padx=2, pady=2, sticky="we") 156 e.focus_set() 157 c = IntVar() 158 Checkbutton(t, text="不区分大小写", variable=c).grid(row=1, column=1, sticky='e') 159 Button(t, text="查找所有", command=lambda: search(v.get(), c.get(), textPad, t, e)).grid\ 160 (row=0, column=2, sticky="e"+"w", padx=2, pady=2) 161 162 def close_search(): 163 textPad.tag_remove("match", "1.0", END) 164 t.destroy() 165 t.protocol("WM_DELETE_WINDOW", close_search) 166 167 168 def mypopup(event): 169 global editmenu 170 editmenu.tk_popup(event.x_root, event.y_root) 171 172 173 def search(needle, cssnstv, textPad, t, e): 174 textPad.tag_remove("match", "1.0", END) 175 count = 0 176 if needle: 177 start = 1.0 178 while True: 179 pos = textPad.search(needle, start, nocase=cssnstv, stopindex=END) 180 if not pos: 181 break 182 strlist = pos.split('.') 183 left = strlist[0] 184 right = str(int(strlist[1])+len(needle)) 185 lastpos = left+'.'+right 186 textPad.tag_add("match", pos, lastpos) 187 count += 1 188 start = lastpos 189 textPad.tag_config('match', background="yellow") 190 e.focus_set() 191 t.title(str(count)+"个被匹配") 192 193 194 def refresh(): 195 global top, filename 196 if filename: 197 top.title(os.path.basename(filename)) 198 else: 199 top.title("记事本") 200 201 202 top = Tk() 203 top.title("记事本") 204 top.geometry("640x480+100+50") 205 206 menubar = Menu(top) 207 208 # 文件功能 209 filemenu = Menu(top) 210 filemenu.add_command(label="新建", accelerator="Ctrl+N", command=new_file) 211 filemenu.add_command(label="打开", accelerator="Ctrl+O", command=open_file) 212 filemenu.add_command(label="保存", accelerator="Ctrl+S", command=save) 213 filemenu.add_command(label="另存为", accelerator="Ctrl+shift+s", command=save_as) 214 filemenu.add_command(label="重命名", accelerator="Ctrl+R", command=rename_file) 215 filemenu.add_command(label="删除", accelerator="Ctrl+D", command=delete) 216 menubar.add_cascade(label="文件", menu=filemenu) 217 218 # 编辑功能 219 editmenu = Menu(top) 220 editmenu.add_command(label="撤销", accelerator="Ctrl+Z", command=undo) 221 editmenu.add_command(label="重做", accelerator="Ctrl+Y", command=redo) 222 editmenu.add_separator() 223 editmenu.add_command(label="剪切", accelerator="Ctrl+X", command=cut) 224 editmenu.add_command(label="复制", accelerator="Ctrl+C", command=copy) 225 editmenu.add_command(label="粘贴", accelerator="Ctrl+V", command=paste) 226 editmenu.add_separator() 227 editmenu.add_command(label="查找", accelerator="Ctrl+F", command=find) 228 editmenu.add_command(label="全选", accelerator="Ctrl+A", command=select_all) 229 menubar.add_cascade(label="编辑", menu=editmenu) 230 231 # 关于 功能 232 aboutmenu = Menu(top) 233 aboutmenu.add_command(label="作者", command=author) 234 aboutmenu.add_command(label="版权", command=power) 235 menubar.add_cascade(label="关于", menu=aboutmenu) 236 237 top['menu'] = menubar 238 239 shortcutbar = Frame(top, height=25, bg='Silver') 240 shortcutbar.pack(expand=NO, fill=X) 241 242 textPad=Text(top, undo=True) 243 textPad.pack(expand=YES, fill=BOTH) 244 scroll=Scrollbar(textPad) 245 textPad.config(yscrollcommand=scroll.set) 246 scroll.config(command=textPad.yview) 247 scroll.pack(side=RIGHT, fill=Y) 248 249 # 热键绑定 250 textPad.bind("<Control-N>", new_file) 251 textPad.bind("<Control-n>", new_file) 252 textPad.bind("<Control-O>", open_file) 253 textPad.bind("<Control-o>", open_file) 254 textPad.bind("<Control-S>", save) 255 textPad.bind("<Control-s>", save) 256 textPad.bind("<Control-D>", delete) 257 textPad.bind("<Control-d>", delete) 258 textPad.bind("<Control-R>", rename_file) 259 textPad.bind("<Control-r>", rename_file) 260 textPad.bind("<Control-A>", select_all) 261 textPad.bind("<Control-a>", select_all) 262 textPad.bind("<Control-F>", find) 263 textPad.bind("<Control-f>", find) 264 265 textPad.bind("<Button-3>", mypopup) 266 top.mainloop()