Tkinterは、tkライブラリを読み込むtclインタプリタのラッパーです。 Tclはいくつかのグローバル変数を使って "単語"とみなすものを定義し、その実装のさまざまな場所でこれらの変数を使用します。最も目立つように、これらは、テキストと入力ウィジェットのマウスとキーのバインディングを処理するために使用されます。
ウィンドウでは、「単語」はスペース以外のものとして定義され、デフォルトでダブルクリックすると「単語」が選択されます。したがって、variable<0
をダブルクリックすると、空白の間のすべてが選択されます。他のプラットフォームでは、 "単語"は大文字と小文字、数字、アンダースコアのみで定義されています。
文字、数字、および下線のみで単語を扱うようにするには、これらのグローバル変数を、それらの文字(または必要な文字)に一致する正規表現に再定義できます。 Windowsで実行している
import tkinter as tk
def set_word_boundaries(root):
# this first statement triggers tcl to autoload the library
# that defines the variables we want to override.
root.tk.call('tcl_wordBreakAfter', '', 0)
# this defines what tcl considers to be a "word". For more
# information see http://www.tcl.tk/man/tcl8.5/TclCmd/library.htm#M19
root.tk.call('set', 'tcl_wordchars', '[a-zA-Z0-9_]')
root.tk.call('set', 'tcl_nonwordchars', '[^a-zA-Z0-9_]')
root = tk.Tk()
set_word_boundaries(root)
text = tk.Text(root)
text.pack(fill="both", expand=True)
text.insert("end", "if (variable<0): return 0;\n")
root.mainloop()
:
次の例では、それが唯一の文字として定義される単語、数字、およびすべてのプラットフォーム用のアンダースコアを強制する必要がありますか? –
はい、windows 10. Python 3.5。 Tkinter 8.6 – funkyKay