Недавно я работал над простым текстовым редактором 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()
Однако я не могу заставить его работать. Предполагается, что код выделяет все слова, которые должны быть найдены, однако пользователь должен нажать на кнопку «Найди меня!!!». столько раз, сколько слов нужно выделить, чтобы выделить их все. искал в Интернете что-нибудь, что могло бы помочь мне исправить эту функцию поиска, но мне не удалось найти никаких объяснений того, как я могу это сделать. Любая помощь в исправлении этой функции поиска будет очень признательна.
РЕДАКТИРОВАТЬ
Это новый код, который до сих пор не решает проблему:
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()
tag
сtag_add
ко всем этим вхождениям. - person nbro   schedule 13.02.2015for targetfind in where: pastit = where + ('+%dc' % len(targetfind))
- person Pseudonym Enigma   schedule 13.02.2015