Hi everybody,


I am experiencing a very weird situation here:

I have a program that uses tkinter and python 3, but its search function isn't working under Windows.

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()

Write something in the text area and then try to use the little entry field to find one of the words you wrote in the text area. You'll see that it just doesn't work, whereas it does work right away under Linux.

Can somebody tell me what the problem is, isn't python suppossed to be platform independent??


Also, if someone has a better suggestion the function itself I have already asked about it in this topic: http://www.daniweb.com/forums/thread273351.html


thank you

Recommended Answers

All 5 Replies

def find():
    start = 1.0
    while 1:
        find = text_to_be_found.get()

It could be that you define "find" as a function and a variable. It could also be that "text_to_be_found" is not within the scope of the function. Print the result of text_to_be_found.get() and see if it is returning anything.

def find():
    start = 1.0
    while 1:
        find = text_to_be_found.get()

It could be that you define "find" as a function and a variable. It could also be that "text_to_be_found" is not within the scope of the function. Print the result of text_to_be_found.get() and see if it is returning anything.

I did what you said and here are the results:

I changed find (variable) to f, then:

f = text_to_be_found.get()
pos = textarea.search(f, start, stopindex=END)

also, I added the following lines (outside the loop):

print(f)
print(pos)

The first print outputs whatever I'm searching for.
The second print prints nohing...

I think the problem is that there is something wrong with pos.

You should be able to roll your own with something along the lines of this, but you may have to account for carriage returns, etc.

f = text_to_be_found.get()
pos = textarea.search(f, start, stopindex=END)
##
##   becomes
f = text_to_be_found.get()
t_area = textarea.get(1.0, END)
pos = t_area.find(f)
if pos > -1:
    print "Found", f, pos

I'll try that...

Nope, it's still not working...

The one I have (which works only under Linux) highlights the strings it finds, all of them at the same time, but your code only displays the position of the term's first character.

I tried to make it highlight the code above (the tag part), but it says invalid operand because pos is now an integer.

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.