我希望能夠在Tkinter Text小部件中雙擊test,
,並讓它選擇測試(並排除逗號)。如何修改Tkinter Text小部件中的當前選擇長度?
這裏是我試過:
import Tkinter as tk
def selection_mod(event=None):
result = aText.selection_get().find(',')
if result > 0:
try:
aText.tag_add("sel", "sel.first", "sel.last-1c")
except tk.TclError:
pass
lord = tk.Tk()
aText = tk.Text(lord, font=("Georgia", "12"))
aText.grid()
aText.bind("<Double-Button-1>", selection_mod)
lord.mainloop()
的第一個問題是<Double-Button-1>
似乎觸發處理程序作出選擇之前,生產:
TclError: PRIMARY selection doesn't exist or form "STRING" not defined
第二個問題是,即使使用有效的綁定,
我的選擇標籤似乎沒有做任何事情。
它甚至沒有提出錯誤,我試過沒有except tk.TclError:
。
我最近發現瞭如何設置用tcl選擇的字符。 [鏈接](http://stackoverflow.com/a/28133301/1217270) – 2015-01-26 23:17:04