Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cấu hình double-click mặc định cho Text widget Tkinter

Text widget Tkinter hỗ trợ nhập nhiều dòng. Cấu hình double-click để chọn từ (word) tại vị trí con trỏ.

Dùng tag_add() và tag_configure(). Bind <Double-1> vào function select_all.

Ví dụ: Disable double-click select word

from tkinter import *

win = Tk()
win.geometry("700x350")

def select_all():
    text.tag_add("start", "1.0", "end")
    return "break"

text = Text(win, width=50, height=10, font=('Calibri 14'))
text.pack()
text.insert(INSERT, "Select a word and then double-click")
text.bind('', select_all)
win.mainloop()

Kết quả

Text widget có text sẵn. Double-click từ bất kỳ → chọn toàn bộ text (tag "start" 1.0 đến end).

Cấu hình double-click mặc định cho Text widget Tkinter

Giải thích: return "break" ngăn chặn default behavior (chọn từ). Mặc định double-click chọn word tại cursor.