G_S 38 Junior Poster in Training

Hi people!


I need your help with a find function for a glossary program that I'm writing for my own use. I'm using python 3.x and have already made a find function, but I don't like how it came out. Let me explain with an example:

First, let me show you the code:

from tkinter import *
import tkinter.ttk

def sort():
    pass

def find():
    start = 1.0
    while 1:
        find = text_to_be_found.get()
        pos = textarea.search(find, start, stopindex=END)
        if not pos:
            break
        beginning = pos+"linestart"
        end = pos+"lineend"
        start = pos + "+1c"
        textarea.tag_add(SEL, beginning, end)

window = tkinter.Tk()
window.title("Help with find function")

mainframe = tkinter.ttk.Frame(window, border=3, relief="sunken") 
textarea = Text(mainframe)
button = tkinter.ttk.Button(mainframe, text="Sort", command=sort)
button2 = tkinter.ttk.Button(mainframe, text="Find", command=find)
text_to_be_found_label = tkinter.ttk.Label(mainframe, text="Find the following word: ",)
text_to_be_found = tkinter.ttk.Entry(mainframe)

mainframe.grid(row=0, column=0)
textarea.grid(row=0, column=0, columnspan=2)
button.grid(row=2, column=0)
button2.grid(row=2, column=1)
text_to_be_found_label.grid(row=3, column=0, pady=5)
text_to_be_found.grid(row=3, column=1, pady=5)

window.mainloop()

now pay attention:

1. run the program
2. write the following:

race
track
racetrack
racetrack creation
horse
shoe
horseshoe
the shoes of the horse

3. now write shoe in the entry box.
4. press find.

See what happens? Now I have two problems with this code:

Firstly, it finds all the lines containing the word, I'm ok with it highlighting the whole line and not the word itself, but the problem is that it finds all the lines AT ONCE. I want it to find the first line containing the word, and then the next one once I press find again (i.e I want it to work like the typical find function you get in all text editors), and so on.

Secondly: if the glossary is too big, it won't take me to the word that it found, so I have to scroll all over the text area trying to find the line thatis highlighted. Is there a way of having python scroll over to that particular word?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.