Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that you handle <code>&lt;KeyRelease&gt;</code> instead of <code>&lt;KeyPress&gt;</code> (or <code>&lt;Return&gt;</code>). </p> <p>At the time the <code>&lt;KeyRelease&gt;</code> event is raised, the text widget has already been updated with the newline. And because text was selected, that selected text got simply replaced with the newline (as it would have been replaced by pressing any other key).</p> <p>So better bind to <code>&lt;KeyPress&gt;</code> or <code>&lt;Return&gt;</code> instead of <code>&lt;KeyRelease&gt;</code>.</p> <hr> <p>I think the easiest way to solve your issue is indeed to handle both <code>&lt;KeyRelease&gt;</code> and <code>&lt;Return&gt;</code> seperatly. </p> <p>(You could get it to work using <code>&lt;KeyPress&gt;</code>, but this would involve checking which key is pressed, manually inserting this key to the text, checking which text is selected, and replacing the selected text; so, it will be rather clunky).</p> <pre><code>def handle_keyrelease(event): global text if event.keysym in validkeysymchars: for x in ['testcommand']: strtocmp = text.get("MARK","end") strtocmp = strtocmp.encode('ascii','ignore') strtocmp = strtocmp.strip() print strtocmp if x.startswith(strtocmp): currpos = text.index(INSERT) text.insert(END,x[len(strtocmp):]) text.tag_add(SEL,currpos,"%s+%dc"%(currpos,len(x)-len(strtocmp))) text.mark_set("insert",currpos) return def handle_return(event): text.tag_remove(SEL,"1.9",END) text.mark_set("insert",END) text.insert(END, "\n") text.insert(END, "command&gt;") text.mark_set("MARK",INSERT) text.mark_gravity("MARK",LEFT) return "break" ... text.bind("&lt;KeyRelease&gt;", handle_keyrelease) text.bind("&lt;Return&gt;", handle_return) ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload