[英]configure tkinter text widget as code editor. select words on doubleclick
我嘗試在python中使用tkinter構建代碼編輯器。 我正在使用文本小部件。 現在我堅持雙擊代碼選擇。 當我有這行: if (variable<0) return 0;
我雙擊variable
他將所有字符從一個空格標記到另一個空格(variable<0)
。
所以我在tkinter庫中搜索了doublick函數,發現了這一點:
bind Text <Double-1> {
set tk::Priv(selectMode) word
tk::TextSelectTo %W %x %y
catch {%W mark set insert sel.first}
}
現在我卡住了。 有人可以幫我編輯嗎? 也許與word
?
Tkinter是tcl解釋器的包裝,該解釋器加載tk庫。 Tcl使用一些全局變量來定義它認為是“單詞”的單詞,並在其實現的各個位置使用這些變量。 最明顯的是,它們用於處理文本和輸入小部件的鼠標和鍵綁定。
在Windows上,“單詞”定義為除空格以外的任何內容,默認情況下,雙擊將選擇一個“單詞”。 因此,雙擊variable<0
選擇空白之間的所有內容。 在其他平台上,“單詞”僅定義為大寫和小寫字母,數字和下划線。
要使tkinter將單詞視為僅由字母,數字和下划線組成的單詞,可以將這些全局變量重新定義為與這些字符(或所需的任何其他字符)匹配的正則表達式。
在以下示例中,對於所有平台,應強制將單詞定義為僅字母,數字和下划線:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.