Monday, 2 September 2013

tkinter prevent deletion of selected text with return key

tkinter prevent deletion of selected text with return key

I am implementing a tkinter text widget based console application and as a
part of auto complete feature, I am observing one problem that on pressing
return key results in the deletion of the selected text. Below example
shows the similar problem :
from Tkinter import *
def getCommand(*args):
global text
text.insert(END, "\n")
text.insert(END, "command>")
x = text.get("1.0",END)
print x
return 'break'
def handle_keyrelease(event):
global text
if event.keysym == "Return":
text.tag_remove(SEL,"1.9",END)
text.mark_set("insert",END)
getCommand()
return 'break'
root = Tk()
text = Text(root)
text.pack()
text.insert(END,"command>")
text.focus()
text.bind("<KeyRelease>", handle_keyrelease)
text.insert(END,"sometext")
text.tag_add(SEL,"1.9",END)
text.mark_set("insert","1.9")
root.mainloop()
In this code, when I hit the return key, I want to get the complete
command sometext, however with the current code only s is retrieved. I
have tried setting the cursor position to end and deletion of selection
tag when the return key event is recieved.

No comments:

Post a Comment