The problem is explained by the comments in the code. It is a fundamental question about the method call...every time I try text_box_name.tk_textBackspace() I get the TclError message. That's why I asked the question without the code...

# When user enters a search word in the +search_box' text widget and presses
# "Enter" on the keyboard, the search word is no longer visible because
# of the new line at the end of the string caused by pressing "enter".
# Hitting backspace on the keyboard brings the keyword back into view,
# so I tried to use the method tk_textBackspace to simulate that and remove
# the 'new line' (so the entry in list_box would again be visible.)

from tkinter import *
root = Tk()

# Keyword Search box (user inputs keywords)
search_box=Text(root, height=1,width=48, padx=5,wrap='word',
font=('helvetica','12','bold'))
search_box.insert(END,"Search box: Enter keywords and press 'Enter'")

# Keywords Search event handler.
def handle_event(event):
    eventDict={'2':'KeyPress','3':'KeyRelease'}
    if event.type=='2' and event.keysym=='Return' :

        search_box.tk_textBackspace()
# the line above doesn't even get to execute...generates the error message:
# _tkinter.TclError: invalid command name "tk_textBackspace"
# but the library for text widget has that as a valid method...
# hence the question.

    pass # the rest of the routine's code goes here

## the code below is my workaround (executes on "enter" key release)
## if event.type == '3' and event.keysym == 'Return':
## search_box.see(0.0) # Make start of Keywords string visible
## return

search_box.bind('<Key>',handle_event)
search_box.bind('<KeyRelease>', handle_event)
search_box.pack(fill='x', expand='yes',pady=2)
mainloop() 

You check for "KeyPress" and the return happens after that so you don't catch it. The solution is to use "KeyRelease". Also, to use your program as is, you would have to delete the contents of the box on the first keystroke so it only contains the search term. I have used a label instead for convenience. Most times, a button is provided that the user clicks and is bound to the callback to avoid this problem.

class TestSearch():
    def __init__(self):
        # Keyword Search box (user inputs keywords)
        root = Tk()
        Label(root, text="Search box: Enter keywords and press 'Enter'").pack()
        self.search_box=Text(root, height=1,width=48, padx=5,wrap='word',
                        font=('helvetica','12','bold'))
#        search_box.insert(END,"Search box: Enter keywords and press 'Enter'")
#        self.search_box.bind('<Key>',self.handle_event)
        self.search_box.bind('<KeyRelease>', self.handle_event)
        self.search_box.pack(fill='x', expand='yes',pady=2)
        self.search_box.focus_set()
        root.mainloop() 

    # Keywords Search event handler.
    def handle_event(self, event):
        eventDict={'2':'KeyPress','3':'KeyRelease'}
        if event.type=='3' and event.keysym=='Return' :
            print "Event"
            search_term = self.search_box.get(1.0, END) 
            print search_term
            search_term = search_term.strip()
            print "adjusted", search_term
            self.search_box.delete(1.0, END) 
            self.search_box.insert(END, search_term)

TS=TestSearch()

And this page says to return "break" which seems to work.

class TestSearch():
    def __init__(self):
        # Keyword Search box (user inputs keywords)
        root = Tk()
        Label(root, text="Search box: Enter keywords and press 'Enter'").pack()
        self.search_box=Text(root, height=1,width=48, padx=5,wrap='word',
                        font=('helvetica','12','bold'))
        self.search_box.bind('<Return>', self.handle_event)
        self.search_box.pack(fill='x', expand='yes',pady=2)
        self.search_box.focus_set()

        root.mainloop() 

    # Keywords Search event handler.
    def handle_event(self, event):
            search_term = self.search_box.get(1.0, END) 
            search_term = search_term.strip()
            print "adjusted", search_term
            return "break"

TS=TestSearch()
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.