-2

I am trying to code a code editor, however, whenever I press the ( key, it inserts ) before the ( text can render onto the text widget.

Here is the code:

main_textbox.bind('(', lambda a: add_char(e=1, char=')'))

And the function:

def add_char(e, char):
    main_textbox.insert("insert", char)
    update_syntax(123) # Does some highlighting
  • Please [edit] your question to provide a [mre] – JRiggles Aug 25 '23 at 17:40
  • 1
    It seems as if you explicitly wrote code to say "when the `(` key is pressed, call my function that adds a `)` character"... and now are *surprised* that a `)` is inserted? What actually is wrong when the code runs? What, concretely, happens; what is supposed to happen instead; and **how is that different**? – Karl Knechtel Aug 25 '23 at 17:46
  • Your binding gets called *before* the built-in binding that actually inserts the character; this allows the character to be modified, or blocked completely. Two options to fix this: 1) insert `()` yourself, and `return "break"` from the binding to prevent the standard insertion from happening; 2) delay your insertion slightly, to let the standard insertion happen first: `main_textbox.after(1, add_char, ...)` perhaps. – jasonharper Aug 25 '23 at 17:50
  • See [this](https://stackoverflow.com/questions/11541262/basic-query-regarding-bindtags-in-tkinter/11542200#11542200) for an explanation about how bindings are processed. – Bryan Oakley Aug 25 '23 at 18:33

0 Answers0