Python Forum
[Tkinter] Problem with tkinter search widget - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Problem with tkinter search widget (/thread-17023.html)



Problem with tkinter search widget - poopcupine - Mar-25-2019

Hi all,

I am trying to highlight similar documents using tkinter GUI.
I have a dictionary that reads from 2 documents and put similar words into the one dictionary, key being the word and value being the occurence of the word.
My tkinter gui will highlight using the text widget search to search for the key in the dictionary, if found in any of the document, it will highlight the word. However, it is not highlighting some of the words and I can't figure out why..

Below is my function for the highighting, compare is my dictionary
from Tkinter import *

#Highlight function to highlight similar words
def highlight(line,text,linenumber,compare):
    #Create text field for doc1 and co2
    print compare
    text.configure(state='normal')
    text.insert(INSERT, line)
    text.pack()
    text.tag_config('same', background='yellow')

    #Iterate through thte dictionaries to look for key and highlight key based of the value of the key.
    for key, value in compare.iteritems():
        pos = '1.0' # <-- position is resetted to 1 in each iteration
        for i in range(value):
            idx = text.search(key, pos, END) #Uses tkinter text widget search to search for dictionary key in .py file1 and .py file2
            if not idx:
                break
            print "key: " + str(len(key))
            pos = '{}+{}c'.format(idx, len(key))
            text.tag_add('same', idx, pos)
when i print my dictionary:
{'lol.csv': 1, 'a': 1, 'c': 1, 'f': 1, 'ahahhahahha': 1, 'haha': 1, 'dog': 1, 'aaaa': 1, '=': 1}

Below is an image of the output I get:
https://www.dropbox.com/s/r3wcnu8z0ko59ah/output.PNG?dl=0


Any help is greatly appreciated


RE: Problem with tkinter search widget - Larz60+ - Mar-25-2019

please post runable code snippet
Also, it's time to seriously think about upgrading. Current version of python is 3.7.2