定义双击文本小部件时选择的字符 [英] defining what characters are selected when double clicking in a Text widget
问题描述
在 Windows 上,双击文本小部件中的单词也将选择连接的标点符号.
有什么办法可以定义你想被选中的字符吗?
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
此变量的值是一个正则表达式,可以设置它来控制被视为单词"字符的内容,例如通过在 Tk 中双击文本来选择一个单词.它是平台依赖.在 Windows 上,它默认为 \S,意思是除了一个Unicode 空格字符.否则它默认为 \w,它是任何Unicode 单词字符(数字、字母或下划线).
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 的示例:
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()
来自 http://wiki.tcl.tk/1655 的注释:
...要更改有效的字符,您必须首先执行类似:
...to change the characters that are valid, you must first do something like:
抓住{tcl_endOfWord}
catch {tcl_endOfWord}
可以在这里研究正则表达式语法:https://www.tcl.tk/man/tcl8.6/TclCmd/re_syntax.htm
The regex syntax can be studied here: https://www.tcl.tk/man/tcl8.6/TclCmd/re_syntax.htm
这篇关于定义双击文本小部件时选择的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!