from
tkinter
import
*
root
=
Tk()
fram
=
Frame(root)
Label(fram, text
=
'Find'
).pack(side
=
LEFT)
edit
=
Entry(fram)
edit.pack(side
=
LEFT, fill
=
BOTH, expand
=
1
)
edit.focus_set()
Find
=
Button(fram, text
=
'Find'
)
Find.pack(side
=
LEFT)
Label(fram, text
=
"Replace With "
).pack(side
=
LEFT)
edit2
=
Entry(fram)
edit2.pack(side
=
LEFT, fill
=
BOTH, expand
=
1
)
edit2.focus_set()
replace
=
Button(fram, text
=
'FindNReplace'
)
replace.pack(side
=
LEFT)
fram.pack(side
=
TOP)
text
=
Text(root)
text.insert(
'1.0'
,
)
text.pack(side
=
BOTTOM)
def
find():
text.tag_remove(
'found'
,
'1.0'
, END)
s
=
edit.get()
if
(s):
idx
=
'1.0'
while
1
:
idx
=
text.search(s, idx, nocase
=
1
,
stopindex
=
END)
if
not
idx:
break
lastidx
=
'% s+% dc'
%
(idx,
len
(s))
text.tag_add(
'found'
, idx, lastidx)
idx
=
lastidx
text.tag_config(
'found'
, foreground
=
'red'
)
edit.focus_set()
def
findNreplace():
text.tag_remove(
'found'
,
'1.0'
, END)
s
=
edit.get()
r
=
edit2.get()
if
(s
and
r):
idx
=
'1.0'
while
1
:
idx
=
text.search(s, idx, nocase
=
1
,
stopindex
=
END)
print
(idx)
if
not
idx:
break
lastidx
=
'% s+% dc'
%
(idx,
len
(s))
text.delete(idx, lastidx)
text.insert(idx, r)
lastidx
=
'% s+% dc'
%
(idx,
len
(r))
text.tag_add(
'found'
, idx, lastidx)
idx
=
lastidx
text.tag_config(
'found'
, foreground
=
'green'
, background
=
'yellow'
)
edit.focus_set()
Find.config(command
=
find)
replace.config(command
=
findNreplace)
root.mainloop()