1

I am in the process of writing a simple GUI text editor using tkinter and python, and while implementing a line and column number readout, I noticed that when I set the update of the readout to a mouse or keyboard event, the column readout always seems to lag 1 event behind what has actually occurred.

The code in question is as follows:

def update_line_num(event):
  line_num, col_num = txt_field.text.index("insert").split('.') # Get the line and column numbers
  status_line_txt.configure(text=f"ln {line_num} : col {col_num}") # Update the UI

  statusbar.draw() # Update the statusbar and it's children.

I then bind this function with the tk.Text.bind("<Event>", function) syntax. In this case, I am binding to the Button and KeyPress events.

However, this results in a column readout which lags behind the true cursor position by 1 event. By this, I mean that when I press a key or click the mouse to a different piece of text, the column readout always displays the location the cursor was just at.

I know its not an issue with the update code for the actual UI, because when I print the line and column numbers out, I get the same incorrect numbers.

Xenon
  • 11
  • 6
  • This is how tkinter bindings are designed to work - bindings on widgets are processed before the default behavior. For a deeper explanation see https://stackoverflow.com/questions/11541262/basic-query-regarding-bindtags-in-tkinter/11542200#11542200 – Bryan Oakley Apr 09 '22 at 05:14
  • @Bryan Oakley ah, thanks. I was not aware of this. – Xenon Apr 09 '22 at 17:36

0 Answers0