python 大作业 自己写了记事本  也参考网上的  查询会有点问题 替换没问题

# encoding=utf-
from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
import os '''打开文件的功能目前不是很完善''' filename = '' def author():
showinfo('helo', '儿子') def power():
showinfo('版权信息', '爸爸') def myopen():
global filename
filename = askopenfilename(defaultextension='.txt')
if filename == '':
filename = None
else:
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() def new():
global root, filename, textPad
root.title('未命名文件')
filename = None
textPad.delete(1.0, END) def save():
global filename
try:
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')
global filename
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():
global textPad
textPad.event_generate('<<Cht>>') def copy():
global textPad
textPad.event_generate('<<Copy>>') def paste():
global textPad
textPad.event_generate('<<Paste>>') def undo():
global textPad
textPad.event_generate('<<Undo>>') def redo():
global textPad
textPad.event_generate('<<Redo>>') def select_all():
global textPad
textPad.event_generate('sel', '1.0', 'end') def find():
global root
t = Toplevel(root)
t.title('查找')
# 设置窗口大小
t.geometry('290x70+200+250')
t.transient(root)
v1=StringVar()
Label(t, text='查找/替换:').grid(row=, column=, sticky='e')
Label(t, text='替换文本:').grid(row=, column=)
Entry(t, width=,textvariable=v1).grid(row=, column= ) v = StringVar()
e = Entry(t, width=, textvariable=v)#替换 e.grid(row=, column=, padx=, pady=, sticky='we')
e.focus_set()
c = IntVar() #Checkbutton(t, text='不区分大小写', variable=c).grid(row=, column=, sticky='e')
Button(t, text='查找所有', command=lambda: search(v.get(), c.get(), textPad, t, e)).grid(row=, column=,sticky='e' + 'w', padx=,pady=)
Button(t, text='替换所有', command=lambda: mytihuan(v1.get(),v.get())).grid(row=, column=, padx=,pady=)
#tihuantext = Text(t, width=, height=) 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 =
if needle:
pos = '1.0'
while True:
pos = textPad.search(needle, pos, nocase=cssnstv, stopindex=END)
if not pos: break
#lastpos=
lastpos = pos + str(len(needle))
#print(str(len(needle))+"-----"+needle)
textPad.tag_add('match', pos, lastpos)
count +=
pos = lastpos
textPad.tag_config('match', foreground='yellow', background='green')
e.focus_set()
t.title(str(count) + '个被匹配') def popup(event):
global editmenu
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=, bg='light sea green')
# shortcutbar.pack(expand=NO, fill=X)
# lnlabel = Label(root, width=, bg='antique white')
# lnlabel.pack(side=LEFT, anchor='nw', fill=Y) textPad = Text(root, width=, height=, selectforeground="black", undo=True, font=)
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('<Control-N>', new)
textPad.bind('<Control-n>', new)
textPad.bind('<Control-O>', myopen)
textPad.bind('<Control-o>', myopen)
textPad.bind('<Control-S>', save)
textPad.bind('<Control-s>', save)
textPad.bind('<Control-A>', select_all)
textPad.bind('<Control-a>', select_all)
textPad.bind('<Control-F>', find)
textPad.bind('<Control-f>', find)
textPad.bind('<Button-3>', popup)
root.mainloop()

最新文章

  1. [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性
  2. JSON数据源提供多值参数的实现
  3. 修复 XE8 for Android 分享图片到 Gmail 权限不足的问题
  4. KMP--路过
  5. C#三大支柱之多态
  6. GLOG使用注意事项
  7. Directx11学习笔记【四】 封装一个简单的Dx11DemoBase
  8. 安装WebLogic的详细步骤
  9. docker(一) Centos7下安装docker
  10. spoj Ae2b
  11. pip问题
  12. 开启hadoop集群
  13. 使用缓存方式优化递归函数与lru_cache
  14. VS 统计整个项目总的代码行数
  15. 禅道项目管理软件&#160;为提交Bug页面添加“优先级”字段
  16. Linux 第五周 实验: 分析system_call中断处理过程
  17. idea中使用tomcat 方式启动spring boot项目
  18. google kaptcha 验证码组件使用简介
  19. [T-ARA][한겨울의 숨바꼭질/숨바꼭질][深冬的捉迷藏/捉迷藏]
  20. 苹果使用蓝汛CDN网络分发ios8

热门文章

  1. c# 字符串比较优化
  2. 【转】python创建和删除文件
  3. hdfs dfs ls /列出了本地根目录下文件夹和文件Warning: fs.defaultFS is not set when running &quot;ls&quot; command
  4. day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)
  5. freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
  6. mysql 命令行个性化设置
  7. css属性选择器: | 与 ~
  8. Java程序基本优化
  9. scrapy 中没有 crawl 命令
  10. 概率图模型(PGM,Probabilistic Graphical Model)