apostrophe not considered with tkinter's wordstart and wordend

Rick Johnson rantingrickjohnson at gmail.com
Sun Jan 25 03:56:31 CET 2015

On Thursday, January 22, 2015 at 4:26:29 PM UTC-6, Christian Gollwitzer wrote:

> Well, it turns out you actually can. We don't have Guido's
> time machine, but still there is a configuration option in
> Tk, albeit a very obscure one: you have to set the global
> Tcl variables tcl_wordchars and tcl_nonwordchars after
> loading Tk to a regexp which defines the word boundaries:
> 	http://wiki.tcl.tk/1655
> Strangely, there is a different preset for Windows and
> X11. The Windows preset - consider everything besides
> space as part of the word - makes more sense to me than
> the Unix default, which only counts letters, numbers and
> underscores as part of the word. To set the Windows
> preset, do this:
> tk.eval('set tcl_wordchars {\S}');
> tk.eval('set tcl_nonwordchars {\s}');
> (untested in Python, only tested directly from a wish shell)

Hmm. Thanks for posting this, but i could not get it to
work??? I tried all sorts of sensible patterns but
nothing works.

Below is a simple testing app with bindings which highlight the
word under the mouse like the OP requested. If you can modify
the word boundaries so that it captures the equivalent of this
pattern r"'\w+'", then i will write a wrapper method for the
Tkinter module for arbitrarily modifying the boundaries. 

Here is the code, happy hacking ;-)


import Tkinter as tk
from Tkconstants import YES, BOTH, END

SIDX = '1.0'
STR = """
My humble dictators,
ask not what the 'boot_lickers' can do for you,
ask what *YOU* can do to for *YOUR* community!

class MyText(tk.Text):
    def __init__(self, master, **kw):
        tk.Text.__init__(self, master, **kw)
        self.bind("<Button-1>", self.evtButtonOneDown)
        self.tag_add('hit', END)
        self.tag_config('hit', background='red')

    def evtButtonOneDown(self, event):
        sLC = self.index('current wordstart')
        eLC = self.index('current wordend')
        print 'hitspan from {0!r} to {1!r}'.format(sLC, eLC)
        self.tag_remove('hit', SIDX, 'end')
        self.tag_add('hit', sLC, eLC)
if __name__ == '__main__':
    root = tk.Tk()
##    root.eval("set tcl_wordchars {psst: over here!}")
##    root.eval("set tcl_nonwordchars {psst: and here too!}")
    et = MyText(root, undo=True)
    et.insert(SIDX, STR)
    et.pack(fill=BOTH, expand=YES)
## END CODE ##

More information about the Python-list mailing list