The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

smacadam (5) [Avatar] Offline
I can't get the method tk_textBackspace to work in any text widget. It is a valid method in the directory, but I get a "tkinter.TclError: invalid command name" error message. Same with tk_textSelectTo. What am I doing wrong.
smacadam (5) [Avatar] Offline
Re: tk_textBackspace in text widget
Resubmit... I haven't heard any reply. Is the call to tk_textBackspace no longer valid? Can anyone help??
woooee (10) [Avatar] Offline
Re: tk_textBackspace in text widget
I can't get it to work means nothing. How about a code snippet? What do you want to do and how is that different from what is happening in the code you posted?

Message was edited by:
smacadam (5) [Avatar] Offline
Re: tk_textBackspace in text widget
Sorry. Here's the test routine. 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',
search_box.insert(END,"Search box: Enter keywords and press 'Enter'")

# Keywords Search event handler.
def handle_event(event):
if event.type=='2' and event.keysym=='Return' :

# 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('<KeyRelease>', handle_event)
search_box.pack(fill='x', expand='yes',pady=2)
woooee (10) [Avatar] Offline
Re: tk_textBackspace in text widget
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.

I can't get the code to show correctly so I have posted it at
smacadam (5) [Avatar] Offline
Re: tk_textBackspace in text widget
Thank you all. Doing a work-around on keyRelease solves the problem, and the program is running fine. But the original question is still on my mind. Why doesn't tk_textBackspace execute--instead it raises a Tcl error. For any text widget, when I try to execute widget.tk_textBackspace I get the following error: "_tkinter.TclError: invalid command name "tk_textBackspace" Has it been removed from Tkinter?
smacadam (5) [Avatar] Offline
Re: tk_textBackspace in text widget
I wish to re-ask this question in a concise way. The code below is a clean test which shows the problem. When the code is run, it asks the user to type a character or characters into the text widget and press return. The return is bound to a single line of code:


When I run this on my machine I get the following error trace:

Exception in Tkinter callback
Traceback (most recent call last):
File "Csmilieython32lib kinter\", line 1399, in __call__
return self.func(*args)
File "C:/Users/Pete/Dropbox/Recipe Project/Pete's Sandbox/", line 10, in handle_event
File "Csmilieython32lib kinter\", line 2820, in tk_textBackspace'tk_textBackspace', self._w)
_tkinter.TclError: invalid command name "tk_textBackspace"

I looked into the library tkinter\ line 2820 and found the following:

def tk_textBackspace(self):'tk_textBackspace', self._w)

which appears to allow the method call as typed. I am running python version 3.2.3 under windows7. The tkinter version that came with the python is Tcl/Tk version 8.5.

I can't see what is wrong with the code and am asking whether the method has been eliminated (ar am I still doing something wrong). I hope this makes the question clear.

P.S. As happened before, this website removes the leading spaces from the code, so it is hard to read. There are only a couple of indents (for the def and if statements), so I hope it will be easy to figure out.

from tkinter import *
root = Tk()
test_widget = Text(root, height=4, width=30, wrap='word')
test_widget.insert("end", "Type any character and press enter.")

def handle_event(event):
# Bind KeyRelease (event type 3) and "Enter" ("Return") key
if event.type=="3" and event.keysym=="Return":

test_widget.bind("<KeyRelease>", handle_event)