apostrophe not considered with tkinter's wordstart and wordend
ravas
ravas at outlook.com
Sat Jan 24 22:17:40 EST 2015
We resolved it over at the comp.lang.tcl group.
It turns out what Christian suggested affects what is selected when you double click a word. He later discovered a different method for producing what I want.
Below is my test code that implements both of these things
(tested with Python 3.4 and PyCharm).
import tkinter
import tkinter.messagebox as messagebox
class Creator(object):
def __init__(self):
root = self.root = tkinter.Tk()
# Main Frame
f_main = tkinter.Frame(root, borderwidth=6, relief='flat')
f_main.grid(row=0, column=0, sticky='nsew')
# Text widget, its font and frame
f_txt = tkinter.Frame(f_main, borderwidth=2, relief="sunken")
f_txt.config(width=768, height=768)
f_txt.grid_propagate(False) # ensures a consistent GUI size
f_txt.pack(padx=4, pady=4, side="bottom", fill="both", expand=True)
my_txt = tkinter.Text(f_txt, name='t')
my_txt.config(undo=True, wrap='word')
my_txt.grid(row=0, column=0, sticky="nsew")
self.text = my_txt
# Scrollbar and config
f_scroll = tkinter.Frame(root, borderwidth=4, relief="groove")
f_scroll.grid(row=0, column=1, sticky='nsew')
scrollbar = tkinter.Scrollbar(f_scroll, command=my_txt.yview)
scrollbar.pack(side='left', fill='y', pady=2, padx=0, expand=True)
my_txt.config(yscrollcommand=scrollbar.set)
scrollbar.columnconfigure(0, weight=1)
# Stretchable
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
f_main.columnconfigure(0, weight=1)
f_main.rowconfigure(0, weight=1)
f_txt.rowconfigure(0, weight=1)
f_txt.columnconfigure(0, weight=1)
my_txt.bind("<ButtonRelease-3>", self.test)
my_txt.focus_set()
def test(self, event=None):
txt = str(self.text)
a = "::tk::TextPrevPos {} current tcl_wordBreakBefore".format(txt)
b = "::tk::TextNextPos {} current tcl_wordBreakAfter".format(txt)
ind1 = self.root.tk.eval(a)
ind2 = self.root.tk.eval(b)
x = self.text.get(ind1, ind2)
messagebox.showinfo('info', x)
# ===========================================================================
# start up
# ===========================================================================
GUI = Creator()
GUI.root.tk.eval("catch {tcl_endOfWord}")
GUI.root.tk.eval("catch {tcl_startOfPreviousWord}")
GUI.root.tk.eval("set tcl_wordchars {[[:alnum:]']}")
GUI.root.tk.eval("set tcl_nonwordchars {[^[:alnum:]']}")
GUI.root.mainloop()
More information about the Python-list
mailing list