记事本python代码_Python编写一个记事本

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 importscrolledtext15 importos16

17 filename=''

18

19

20 defauthor():21 showinfo(title="作者", message="water66")22

23

24 defpower():25 showinfo(title="版权信息", message="water66")26

27

28 def new_file(*args):29 globaltop, filename, textPad30 top.title("未命名文件")31 filename =None32 textPad.delete(1.0, END)33

34

35 def open_file(*args):36 globalfilename37 filename = askopenfilename(defaultextension=".txt")38 if filename == "":39 filename =None40 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 defclick_open(event):49 globalfilename50 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 globalfilename59 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 globalfilename70 f = asksaveasfilename(initialfile="未命名.txt", defaultextension=".txt")71 filename =f72 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 defrename(newname):80 globalfilename81 name =os.path.basename(os.path.splitext(filename)[0])82 oldpath =filename83 newpath = os.path.dirname(oldpath)+'/'+newname+'.txt'

84 os.rename(oldpath, newpath)85 filename =newpath86 refresh()87

88

89 def rename_file(*args):90 globalfilename91 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 globalfilename, top107 choice = askokcancel('提示', '要执行此操作吗')108 ifchoice:109 ifos.path.exists(filename):110 os.remove(filename)111 textPad.delete(1.0, END)112 top.title("记事本")113 filename = ''

114

115

116 defcut():117 globaltextPad118 textPad.event_generate("<>")119

120

121 defcopy():122 globaltextPad123 textPad.event_generate("<>")124

125

126 defpaste():127 globaltextPad128 textPad.event_generate("<>")129

130

131 defundo():132 globaltextPad133 textPad.event_generate("<>")134

135

136 defredo():137 globaltextPad138 textPad.event_generate("<>")139

140

141 defselect_all():142 globaltextPad143 textPad.tag_add("sel", "1.0", "end")144

145

146 def find(*agrs):147 globaltextPad148 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 defclose_search():163 textPad.tag_remove("match", "1.0", END)164 t.destroy()165 t.protocol("WM_DELETE_WINDOW", close_search)166

167

168 defmypopup(event):169 globaleditmenu170 editmenu.tk_popup(event.x_root, event.y_root)171

172

173 defsearch(needle, cssnstv, textPad, t, e):174 textPad.tag_remove("match", "1.0", END)175 count =0176 ifneedle:177 start = 1.0

178 whileTrue:179 pos = textPad.search(needle, start, nocase=cssnstv, stopindex=END)180 if notpos:181 break

182 strlist = pos.split('.')183 left =strlist[0]184 right = str(int(strlist[1])+len(needle))185 lastpos = left+'.'+right186 textPad.tag_add("match", pos, lastpos)187 count += 1

188 start =lastpos189 textPad.tag_config('match', background="yellow")190 e.focus_set()191 t.title(str(count)+"个被匹配")192

193

194 defrefresh():195 globaltop, filename196 iffilename: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'] =menubar238

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("", new_file)251 textPad.bind("", new_file)252 textPad.bind("", open_file)253 textPad.bind("", open_file)254 textPad.bind("", save)255 textPad.bind("", save)256 textPad.bind("", delete)257 textPad.bind("", delete)258 textPad.bind("", rename_file)259 textPad.bind("", rename_file)260 textPad.bind("", select_all)261 textPad.bind("", select_all)262 textPad.bind("", find)263 textPad.bind("", find)264

265 textPad.bind("", mypopup)266 top.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值