# encoding=utf-8
from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *import os'''打开文件的功能目前不是很完善'''filename= ''def author():
showinfo('helo', '儿子')
def power():
showinfo('版权信息', '爸爸')
def myopen():globalfilename
filename= askopenfilename(defaultextension='.txt')if filename == '':
filename=Noneelse:
root.title('linlin-note' +os.path.basename(filename))
textPad.delete(1.0, END)
f= open(filename, 'r')
textPad.insert(1.0, f.read())
f.close()
defnew():globalroot, filename, textPad
root.title('未命名文件')
filename=None
textPad.delete(1.0, END)
def save():globalfilenametry:
f= open(filename, 'w')
msg= textPad.get(1.0, 'end')
f.write(msg)
f.close()
except:
saveas()
def saveas():
f= asksaveasfilename(initialfile='未命名.txt', defaultextension='.txt')globalfilename
filename=f
fh= open(f, 'w')
msg= textPad.get(1.0, END)
fh.write(msg)
fh.close
root.title('linlin 记事本' +os.path.basename(f))
def cut():globaltextPad
textPad.event_generate('<>')
def copy():globaltextPad
textPad.event_generate('<>')
def paste():globaltextPad
textPad.event_generate('<>')
def undo():globaltextPad
textPad.event_generate('<>')
def redo():globaltextPad
textPad.event_generate('<>')
def select_all():globaltextPad
textPad.event_generate('sel', '1.0', 'end')
def find():globalroot
t=Toplevel(root)
t.title('查找')
# 设置窗口大小
t.geometry('290x70+200+250')
t.transient(root)
v1=StringVar()
Label(t, text='查找/替换:').grid(row=0, column=0, sticky='e')
Label(t, text='替换文本:').grid(row=1, column=0)
Entry(t, width=20,textvariable=v1).grid(row=1, column=1)
v=StringVar()
e= Entry(t, width=20, textvariable=v)#替换
e.grid(row=0, column=1, padx=2, pady=2, sticky='we')
e.focus_set()
c=IntVar()
#Checkbutton(t, text='不区分大小写', variable=c).grid(row=1, column=1, sticky='e')
Button(t, text='查找所有', command=lambda: search(v.get(), c.get(), textPad, t, e)).grid(row=0, column=2,sticky='e' + 'w', padx=2,pady=2)
Button(t, text='替换所有', command=lambda: mytihuan(v1.get(),v.get())).grid(row=1, column=2, padx=2,pady=2)
#tihuantext= Text(t, width=3, height=2)
def close_search():
textPad.tag_remove('match', '1.0', END)
t.destroy()
t.protocol('WM_DELETE_WINDOW', close_search)
def mytihuan(tihuanwenben,yuanshiwenben):
showinfo('helo', "替换成功")
find_data=yuanshiwenben.strip()
replace_data=tihuanwenben.strip()
data= textPad.get(1.0,END)
print("finddata"+find_data)
data=data.replace(find_data, replace_data)
textPad.delete(1.0,END)
textPad.insert(1.0,data)
#textPad.mark_set(data)
def search(needle, cssnstv, textPad, t, e):
textPad.tag_remove('match', '1.0', END)
count= 0
ifneedle:
pos= '1.0'
whileTrue:
pos= textPad.search(needle, pos, nocase=cssnstv, stopindex=END)if not pos: break#lastpos=0lastpos= pos +str(len(needle))
#print(str(len(needle))+"-----"+needle)
textPad.tag_add('match', pos, lastpos)
count+= 1pos=lastpos
textPad.tag_config('match', foreground='yellow', background='green')
e.focus_set()
t.title(str(count)+ '个被匹配')
def popup(event):globaleditmenu
editmenu.tk_popup(event.x_root, event.y_root)
root=Tk()
root.title('记事本')
root.geometry('800x800+100+100')
menubar=Menu(root)
filemenu= Menu(menubar,tearoff=False)#等于false 不显示上面的-------filemenu.add_command(label='新建', accelerator='Ctrl+N', command=new)
filemenu.add_command(label='打开', accelerator='Ctrl+O', command=myopen)
filemenu.add_command(label='保存', accelerator='Ctrl+S', command=save)
filemenu.add_command(label='另存为', accelerator='Ctrl+Shift+S', command=saveas)
menubar.add_cascade(label='文件', menu=filemenu)
editmenu= Menu(menubar,tearoff=False)#等于false 不显示上面的-------editmenu.add_command(label='撤销', accelerator='Ctrl+Z', command=undo)
editmenu.add_command(label='重做', accelerator='Ctrl+Y', command=redo)
editmenu.add_separator()
editmenu.add_command(label='剪切', accelerator='Ctrl+X', command=cut)
editmenu.add_command(label='复制', accelerator='Ctrl+C', command=copy)
editmenu.add_command(label='粘贴', accelerator='Ctrl+V', command=paste)
editmenu.add_separator()
editmenu.add_command(label='查找/替换', accelerator='Ctrl+F', command=find)
editmenu.add_command(label='全选', accelerator='Ctrl+A', command=select_all)
menubar.add_cascade(label='编辑', menu=editmenu)
aboutmenu= Menu(menubar,tearoff=False)#等于false 不显示上面的-------aboutmenu.add_command(label='作者', command=author)
aboutmenu.add_command(label='版权', command=power)
menubar.add_cascade(label='关于', menu=aboutmenu)
root.config(menu=menubar)
# root['menu'] =menubar
# shortcutbar= Frame(root, height=25, bg='light sea green')
# shortcutbar.pack(expand=NO, fill=X)
# lnlabel= Label(root, width=2, bg='antique white')
# lnlabel.pack(side=LEFT, anchor='nw', fill=Y)
textPad= Text(root, width=90, height=40, selectforeground="black", undo=True, font=50)
textPad.pack(expand=YES, fill=BOTH)
scroll=Scrollbar(textPad)
textPad.config(yscrollcommand=scroll.set)
scroll.config(command=textPad.yview)
scroll.pack(side=RIGHT, fill=Y)
textPad.bind('', new)
textPad.bind('', new)
textPad.bind('', myopen)
textPad.bind('', myopen)
textPad.bind('', save)
textPad.bind('', save)
textPad.bind('', select_all)
textPad.bind('', select_all)
textPad.bind('', find)
textPad.bind('', find)
textPad.bind('', popup)
root.mainloop()