ไม่สามารถรับฟังก์ชันการค้นหาเพื่อเน้นอินสแตนซ์ทั้งหมดของเป้าหมายได้ (โปรแกรมแก้ไขข้อความ)

เมื่อเร็ว ๆ นี้ฉันได้ทำงานกับโปรแกรมแก้ไขข้อความธรรมดา 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()

person Pseudonym Enigma    schedule 13.02.2015    source แหล่งที่มา
comment
เหตุใดคุณจึงใช้ฟังก์ชันภายในฟังก์ชันอื่น ข้อได้เปรียบคืออะไร?   -  person nbro    schedule 13.02.2015
comment
@Rinzler: ฟังก์ชั่นภายในคือการเรียกกลับ ฉันไม่คิดว่านั่นคือปัญหา   -  person Blckknght    schedule 13.02.2015
comment
วิธีการค้นหาจะส่งคืนเพียงดัชนีของรายการแรกที่เกิดขึ้นในวิดเจ็ตข้อความ ดังนั้นคุณต้องคลิกหลายครั้งตามที่คุณบอกเพื่อให้ไฮไลต์ ขั้นแรกให้ลองค้นหาเหตุการณ์ทั้งหมดในวิดเจ็ตข้อความของเป้าหมายที่ต้องการ จากนั้นใช้วิธี tag กับ tag_add กับเหตุการณ์เหล่านี้ทั้งหมด   -  person nbro    schedule 13.02.2015
comment
ฉันดูเหมือนจะไม่สามารถจัดการได้ ฉันเพิ่ม for loop ไว้เหนือโค้ดที่กำหนด Pastit แต่ข้อแตกต่างเพียงอย่างเดียวคือเน้นเฉพาะอักษรตัวแรกของคำเท่านั้น ส่วนที่แก้ไขของโค้ดคือ for targetfind in where: pastit = where + ('+%dc' % len(targetfind))   -  person Pseudonym Enigma    schedule 13.02.2015
comment
@PseudonymEnigma ดูตัวอย่างนี้โดย Bryan   -  person nbro    schedule 13.02.2015
comment
จากตัวอย่างนี้ ฉันยังคงไม่สามารถใช้งานฟังก์ชันได้ ฉันได้ต่อท้ายโค้ดที่อัปเดต (แต่ยังใช้งานไม่ได้) ต่อท้ายคำถามเดิมของฉัน   -  person Pseudonym Enigma    schedule 13.02.2015
comment
@Rinzler ฉันได้อัปเดตโค้ดในคำถามของฉันเพื่อสะท้อนถึงการเปลี่ยนแปลงที่ฉันทำ (ขอบคุณลิงก์ที่คุณให้ไว้) มันยังใช้งานไม่ได้ ผมทำอะไรผิดหรือเปล่า?   -  person Pseudonym Enigma    schedule 14.02.2015
comment
ฉันได้อัปเดตโค้ดอีกครั้งแล้ว ตอนนี้ มันให้พิกัดแก่อินสแตนซ์ทั้งหมดของเป้าหมาย แต่จะเน้นเฉพาะอันสุดท้ายเท่านั้น ฉันควรแก้ไขปัญหานี้อย่างไร? นอกจากนี้ ฉันขอโทษที่ย้ายการสนทนานี้ไปแชทโดยไม่ได้ตั้งใจ ฉันไม่ได้ความว่าอย่างนั้น.   -  person Pseudonym Enigma    schedule 14.02.2015
comment
@PseudonymEnigma: มันแสดงเฉพาะอันสุดท้ายเพราะแต่ละครั้งที่คุณทำการวนซ้ำคุณจะลบแท็ก SEL ทั้งหมด   -  person Bryan Oakley    schedule 14.02.2015
comment
ขอบคุณ ตอนนี้มันใช้งานได้ เนื่องจากไม่มีคำตอบ ฉันจะโพสต์รหัสคงที่เป็นคำตอบของฉันเอง   -  person Pseudonym Enigma    schedule 14.02.2015


คำตอบ (1)


เนื่องจากคำถามของฉันได้รับคำตอบผ่านความคิดเห็นที่เป็นประโยชน์มากมาย ฉันจึงตัดสินใจโพสต์รหัสคงที่ของฉันเป็นคำตอบ

นี่คือรหัส:

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()
person Pseudonym Enigma    schedule 14.02.2015