简体   繁体   中英

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

On Windows, double clicking a word in a Text widget will also select connected punctuation.
Is there any way to define the characters that you want to be 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).

Here is an example for 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()

A note from http://wiki.tcl.tk/1655 :

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

catch {tcl_endOfWord}

The regex syntax can be studied here: https://www.tcl.tk/man/tcl8.6/TclCmd/re_syntax.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM