Python Forum
tkinter search box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter search box
I am attempting to find the correct way to put a search entry in a piece of code I have written. The entry widget would hold the input as a StringVar() that would be searched in a text widget and then highlight the word when found.

I have a reasonable idea of what I have to do and part of how I need to do it but I have not been able to get it to come together in a working form yet.

I have done some looking online for information and managed to find limited information and a couple of examples but I have not been able to make my search code work yet.

The text widget is Code_Snippet:
Code_Snippet = Text(root, bg = '#FFFFDF', fg='#000000', bd=0, width=117, height=27, relief=SUNKEN, cursor='hand2'),y=58)
The Entry widget is search_entry:
search_entry = Entry(root,bd = 2, textvariable = Search, width = '25', bg='#FAFAFA', cursor='hand2'),y=32)
Search routine:
Search = StringVar()
pos =, "1.0", stopindex="end", count=Search)
Code_Snippet.tag_configure('search', background="light blue")
Code_Snippet.tag_add('search',  pos, "%s + %sc" (pos, Search.get()
I am having issues with this and could use some advice on how to correct the code so it will actually find the word and highlight it once it is found.
"Often stumped... But never defeated."
I made a simple dictionary found here that searches for a word and outputs the definition in blue, examples in purple and lemmas in red. You'll need the nltk(natural language tool kit) module to run it. Looks like you're missing a step insert into the text, best of luck
Quote:Hi Joe_momma, I have checked the code that you posted and I have adapted it to my needs however it is not yet working. The problem I am facing is trying to read the data that is loaded into the Text widget. When I ran my code with the adapted portion of your code I get the following error:
Traceback (most recent call last):
File "C:\Users\ve3je\AppData\Local\Programs\Python\Python37\lib\tkinter\", line 1705, in __call__
return self.func(*args)
File "C:\Users\ve3je\Documents\Python Projects\PBSnippet\PBSnippet-5 Cat", line 83, in highlight
defs= Code_Snippet() #feed entry to dictionary
TypeError: 'Text' object is not callable
Please keep this to your original post-
in your first post you declare code_snippet as a text widget
Quote:Code_Snippet = Text(root, bg = '#FFFFDF', fg='#000000', bd=0, width=117, height=27, relief=SUNKEN, cursor='hand2')
in your error defs= code_snippet() code_snippet is a widget not a function that's why your getting an error, post your code or at least a workable section- when i said you are missing the step insert you should have a line that looks like this
Code_Snippet.insert('end','some-text','search') #(location, text or entry, tag)
After a lot of additional reading and various trials I now have it working as I needed it, thank you for the help.
"Often stumped... But never defeated."

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] add search bar = search for input in all computer directory francisco_neves2020 15 6,608 Apr-14-2019, 07:29 PM
Last Post: francisco_neves2020
  [Tkinter] Problem with tkinter search widget poopcupine 1 1,667 Mar-25-2019, 08:24 AM
Last Post: Larz60+
  Python, Tkinter. How to search a specific file in a folder Roo 3 1,822 Feb-13-2019, 10:41 AM
Last Post: Larz60+
  [Tkinter] THow to search a string in a file and display it in listbox by Tkinter mosavit 2 3,948 Jun-21-2017, 08:02 AM
Last Post: buran

Forum Jump:

User Panel Messages

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