Python Tkinter Scrolling Two Text Widgets At The Same Time With Arrow Keys
I'm building a GUI that have 2 text widgets. (I mean it has a bunch of things in it but the sake of this question lets leave it at 2 text widgets). What I want to do is that when I
Solution 1:
Instead of trying to duplicate what the arrow key does, a different method would be to sync the two windows after the key has been processed (ie: set the yview of one to the yview of the other)? You can move the insertion cursor at the same time if you want. This technique will only work if the two widgets have the same number of lines.
While the right way would be to adjust the bindtags so that you create a binding after the class bindings, you can avoid that complication with the knowledge that tkinter processes the key press events. This means you can add bindings to key release events. It yields a tiny lag though.
It would look something like this:
descriptionTextField("<KeyRelease-Up>", OnArrow)
descriptionTextField("<KeyRelease-Down>", OnArrow)
pnTextField("<KeyRelease-Up>", OnArrow)
pnTextField("<KeyRelease-Down>", OnArrow)
...
def OnArrow(event):
widget = event.widget
other = pnTextField if widget == descriptionTextField else descriptionTextField
other.yview_moveto(widget.yview()[0])
other.mark_set("insert", widget.index("insert"))
Using bindtags eliminates the lag. You can set it up like this:
for widget in (descriptionTextField, pnTextField):
bindtags = list(widget.bindtags())
bindtags.insert(2, "custom")
widget.bindtags(tuple(bindtags))
widget.bind_class("custom", "<Up>", OnArrow)
widget.bind_class("custom", "<Down>", OnArrow)
Post a Comment for "Python Tkinter Scrolling Two Text Widgets At The Same Time With Arrow Keys"