Python Forum
[Tkinter] Problem with tkinter search widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Problem with tkinter search widget
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.insert(INSERT, line)
    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 =, pos, END) #Uses tkinter text widget search to search for dictionary key in .py file1 and .py file2
            if not idx:
            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:

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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter Exit Code based on Entry Widget Nu2Python 6 195 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  tkinter text widget word wrap position chrisdb 6 2,254 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  Python3 tkinter radiobutton problem Nick_tkinter 14 1,542 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 1,117 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  Tkinter - How can I extend a label widget? TurboC 2 982 Oct-13-2020, 12:15 PM
Last Post: zazas321
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 795 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 1,152 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  Tkinter problem DPaul 6 1,607 May-28-2020, 03:40 PM
Last Post: DPaul
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 2,280 May-06-2020, 06:11 PM
Last Post: karolp
  Tkinter calendar widget scratchmyhead 4 2,086 May-03-2020, 07:01 PM
Last Post: scratchmyhead

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020