最近我一直在研究GUI python纯文本编辑器。代码调用此函数:
def Find(event=None):
def find_button_pressed():
targetfind = e1.get()
print targetfind
if targetfind:
where = textPad.search(targetfind, INSERT, END)
if where:
print where
pastit = where + ('+%dc' % len(targetfind))
#self.text.tag_remove(SEL, '1.0', END)
textPad.tag_add(SEL, where, pastit)
for targetfind in where:
textPad.mark_set(INSERT, pastit)
textPad.see(INSERT)
textPad.focus()
win = Toplevel()
Label(win, text="Find:").pack()
e1 = Entry(win)
e1.pack()
Button(win, text="Find Me!!!!", command=find_button_pressed).pack()
textPad.focus()
然而,我无法让它发挥作用。代码应该突出显示应该找到的所有单词,但是,用户必须单击按钮" Find Me !!!"为了突出显示所有这些词,有很多次要突出显示。已经在互联网上搜索任何可能有助于我修复此查找功能的内容,但我没有成功找到有关如何执行此操作的任何解释。任何帮助修复此查找功能都将非常感激。
修改
这是仍然无法解决问题的新代码:
def Find(event=None):
def find_button_pressed():
start = "1.0"
end = "end"
start = textPad.index(start)
end = textPad.index(end)
count= Tkinter.IntVar()
count=count
textPad.mark_set("matchStart", start)
textPad.mark_set("matchEnd", start)
textPad.mark_set("searchLimit", end)
targetfind = e1.get()
print targetfind
if targetfind:
while True:
where = textPad.search(targetfind, "matchEnd", "searchLimit",
count=count)
if where == "": break
elif where:
print where
pastit = where + ('+%dc' % len(targetfind))
textPad.tag_remove(SEL, '1.0', END)
textPad.mark_set("matchStart", where)
textPad.mark_set("matchEnd", "%s+%sc" % (where, count.get()))
textPad.tag_add(SEL, where, pastit)
textPad.see(INSERT)
textPad.focus()
win = Toplevel()
Label(win, text="Find:").pack()
e1 = Entry(win)
e1.pack()
Button(win, text="Find Me!!!!", command=find_button_pressed).pack()
textPad.focus()
答案 0 :(得分:0)
由于我的问题通过大量有用的评论得到解答,我决定将我的固定代码作为答案发布。
这是代码:
def Find(event=None):
def find_button_pressed():
start = "1.0"
end = "end"
start = textPad.index(start)
end = textPad.index(end)
count= Tkinter.IntVar()
count=count
textPad.mark_set("matchStart", start)
textPad.mark_set("matchEnd", start)
textPad.mark_set("searchLimit", end)
targetfind = e1.get()
if targetfind:
while True:
where = textPad.search(targetfind, "matchEnd", "searchLimit",
count=count)
if where == "": break
elif where:
pastit = where + ('+%dc' % len(targetfind))
textPad.mark_set("matchStart", where)
textPad.mark_set("matchEnd", "%s+%sc" % (where, count.get()))
textPad.tag_add(SEL, where, pastit)
textPad.see(INSERT)
textPad.focus()
win.destroy()
textPad.tag_remove(SEL, '1.0', END)
win = Toplevel()
Label(win, text="Find:").pack()
e1 = Entry(win)
e1.pack()
Button(win, text="Find Me!!!!", command=find_button_pressed).pack()
textPad.focus()