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
#1
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/r3wcnu8z0ko59a...t.PNG?dl=0


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


Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 836 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  [Tkinter] Search data in treeview without search button TomasSanchexx 3 1,588 Aug-12-2023, 03:17 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,814 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,961 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  tkinter text widget word wrap position chrisdb 6 7,526 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  Python3 tkinter radiobutton problem Nick_tkinter 14 5,979 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 3,553 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  Tkinter - How can I extend a label widget? TurboC 2 2,764 Oct-13-2020, 12:15 PM
Last Post: zazas321
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,192 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 3,484 Sep-29-2020, 05:34 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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