如何通过按钮点击高亮(选择)Text小部件中的文本?
我正在使用Tkinter 8.5和Python 3.3,我希望我的用户能够通过点击一个按钮来复制Text控件中的文本。我已经实现了这个功能,但我还想通过高亮显示(选择)文本来让用户更直观地看到。
以下是一些示例代码:
from tkinter import *
def copy():
root.clipboard_clear()
root.clipboard_append(entry.get(0.0, END))
entry.select_all() # though I wish it did, this attribute doesn't exist!
root = Tk()
entry = Text(root)
entry.pack()
button = Button(root, text="Copy your text", command=copy)
button.pack()
有没有简单的方法可以做到这一点?
1 个回答
4
尝试一下
entry.tag_add('sel', '1.0', 'end')
或者
entry.tag_add('sel', '1.0', 'end-1c')