python记事本实现查询替换
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()
python记事本实现查询替换的更多相关文章
- python字符串截取与替换的例子
python字符串截取与替换的多种方法 时间:2016-03-12 20:08:14来源:网络 导读:python字符串截取与替换的多种方法,以冒号分隔的字符串的截取方法,python字符串替换方法, ...
- 用Python将绝对URL替换成相对URL的代码
下面的内容内容是关于用Python将绝对URL替换成相对URL的内容,应该是对码农有些用途. #!/usr/bin/env python### author : cold night# email : ...
- Linux批量查询替换字符串
Linux 批量查询替换文本文件中的字符串: 1.批量查找某个目下文件的包含的内容,例如: # grep -rn "要找查找的文本" ./ 2.批量查找并替换文件内容. # ...
- PHPStorm 使用正则批量查询替换并自动转换大小写的方法
PHPStorm 的项目查询替换功能那是非常非常强大的, 速度也很快, 配合正则更加灵活强大. 一般的正则查询替换没什么太多好说的, 这里主要说说比较少用的 大小写自动转换的问题, 也是比较少用但很有 ...
- python 连接数据库,查询结果写入数据到excel
使用Python链接数据库查询数据,并将查询结果写入到Excel中,实现方法上主要有两步,第一,查数据,第二,写Excel. 一.导入需要的包 import time import xlwt from ...
- Python实现单词查询&文件查找
最近学C++ Primer,做到第十二章有个习题.要求针对英文文本,对于用户想搜索的单词,打印出该单词在文本中出现的总次数,单词所出现行号及对应的行内容:单词在一行内出现多次,只打印该行一次.C++的 ...
- Python 实现火车票查询工具
注意:由于 12306 的接口经常变化,课程内容可能很快过期,如果遇到接口问题,需要根据最新的接口对代码进行适当修改才可以完成实验. 一.实验简介 当你想查询一下火车票信息的时候,你还在上 12306 ...
- pythonのsqlalchemy简单查询
#!/usr/bin/env python import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.dec ...
- python 小程序,替换文件中的字符串
[root@PythonPC ~]# cat passwd root:x:::root:/root:/bin/bash bin:x:::bin:/bin:/sbin/nologin daemon:x: ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:变量赋值
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Netsparker介绍
Netsparker是一款综合型的web应用安全漏洞扫描工具,它分为专业版和免费版,免费版的功能也比较强大.Netsparker与其他综合性的web应用安全扫描工具相比的一个特点是它能够更好的检测SQ ...
- 【原】Linux中常见服务介绍
1.SSH介绍 简单说,SSH(Secure Shell Protocol)是一种网络协议,用于计算机之间的加密登录.在默认状态下SSH服务提供俩个服务功能,一个是提供类似telnet远程联机服务器的 ...
- warning:Pointer is missing a nullability type specifier (__nonnull or __nullable)
当我们定义某个属性的时候 如果当前使用的编译器版本比较高(6.3+)的话经常会遇到这样一个警告:warning:Pointer is missing a nullability type speci ...
- Go语言的map
map一般是以库的方式提供,在C++和C#和JAVA中都需要引用相应的库而Go语言不需要引入库,可以直接方便使用 定义:map是一堆键值对的未排序集合.无序 1.声明变量: map的声明基本上没有多余 ...
- C语言笔记 14_标准库&assert&ctype&errno&float&limits
C 标准库 <assert.h> 简介 C 标准库的 assert.h头文件提供了一个名为 assert 的宏,它可用于验证程序做出的假设,并在假设为假时输出诊断消息. 已定义的宏 ass ...
- swoole之内存
一.代码 <?php // 可以用来数据共享 // 执行完后 自动释放 // 创建内存表 $table = new swoole_table(1024); // 内存表增加一列 $table-& ...
- JS echarts统计
柱状图 function drawbarFunc(xs, ys) { //var xs1 = []; //var ys1 = []; require.config({ paths: { echarts ...
- intent 跳转
一.不需要返回值的跳转 Intent intent=new Intent(); intent.setClass(目前的acitivy.this, 目标activity.class); startAct ...
- 图的数据结构的实现与遍历(DFS,BFS)
//图的存储结构:const int MAXSIZE = 10;//邻接矩阵template<class T>class MGraph {public: MGraph(T a[], ...