ruby / tk

Tk interface module using tcltklib
Other
118 stars 22 forks source link

Tk::Text listening to a tag covering 1.0 to end stops working after clearing all text (on Mac) #40

Closed AndyObtiva closed 2 years ago

AndyObtiva commented 2 years ago

In light of the solution to https://github.com/ruby/tk/issues/36, I wrote code that monitored key presses upon moving around a Tk::Text widget with the arrow keys. It basically registers a listener to <KeyPress> event on a pre-added tag that covers a range of index 1.0 to end.

Here is a code example of this:

require 'tk'

@root = Tk::Root.new

@text = Tk::Text.new(@root).grid
@text.value = "Some giberish repeated many times\n"*20
@text.tag_add('all', '1.0', 'end')
@text.tag_bind('all', 'KeyPress', ->(event) {puts 'key press', event.inspect})

@root.mainloop

You can navigate around the Tk::Text widget using the arrow keys with no problem. It always fires the <KeyPress> listener anywhere in the 1.0 to end range, printing the event information.

Trouble starts once I clear all the text by highlighting with CMD+A and then hitting DELETE. Afterwards, when I type new text and then move the arrow keys around, no firing of the <KeyPress> event happens anymore.

The plot thickens however. If I insert enough new lines, it sometimes starts firing again, albeit temporarily, and then it stops again.

Either way, the functionality seems broken. It is probably broken in ActiveTcl, not in the Ruby code, but I thought I'd report here just in case there is an issue in the Ruby code.

p.s. I am using ActiveTcl-8.6.9.8609.2-macosx10.9-x86_64-93b04018

AndyObtiva commented 2 years ago

OK, good news! It is not Ruby's fault! I recreated the same exact problem with Python and TKinter:

from tkinter import *
from tkinter import ttk

root = Tk()

text = Text(root)
text.grid()
text.insert('1.0', "Some giberish\nMore giberish\nNot well spelled giberish")
text.tag_add('all', '1.0', 'end')
def print_info(event):
  print('key press')
  print(event)

text.tag_bind('all', '<KeyPress>', print_info)

root.mainloop()
AndyObtiva commented 2 years ago

I reported at the ActiveState Community Forum (not sure where else to report): https://community.activestate.com/t/tk-text-tag-bind-issue-with-activetcl-8-6-occurs-when-using-tk-in-both-ruby-and-python/7665

jeremyevans commented 2 years ago

I tried your example ruby program and can reproduce the issue using tk-8.5.19 on OpenBSD. So it doesn't appear to be specific to ActiveState's distribution, it's probably an issue in tk itself.

Since this doesn't appear to be a bug in ruby-tk, I'm going to close this now.

AndyObtiva commented 2 years ago

For those who may be interested to know, I would like to note that I reported this as an official Tcl/Tk issue: https://core.tcl-lang.org/tk/tktview/631a0b2d95b8426a8851829b16a9d77b9f79b03e