定义在文本小部件中双击时选择的字符

defining what characters are selected when double clicking in a Text widget

在 Windows 上,双击文本小部件中的单词也会 select 连接标点符号。
有什么方法可以定义您想要 selected 的字符吗?

tcl_wordchars
The value of this variable is a regular expression that can be set to control what are considered "word" characters, for instances like selecting a word by double-clicking in text in Tk. It is platform dependent. On Windows, it defaults to \S, meaning anything but a Unicode space character. Otherwise it defaults to \w, which is any Unicode word character (number, letter, or underscore).

这是 Python 3.4 的示例:

import tkinter

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 and frame
        f_txt = tkinter.Frame(f_main, borderwidth=2, relief="sunken")
        f_txt.config(width=768, height=768)
        f_txt.pack(padx=4, pady=4, side="bottom", fill="both", expand=True)

        my_txt = self.text = tkinter.Text(f_txt)
        my_txt.config(undo=True, wrap='word')
        my_txt.grid(row=0, column=0, sticky="nsew")
        my_txt.focus_set()

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()

来自 http://wiki.tcl.tk/1655 的注释:

...to change the characters that are valid, you must first do something like:

catch {tcl_endOfWord}

可以在这里研究正则表达式语法:https://www.tcl.tk/man/tcl8.6/TclCmd/re_syntax.htm