我正在使用 Tkinter 8.5 和 Python 3.3,我希望我的用户能够通过单击按钮来复制文本小部件中的文本。我已经使该部分正常工作,但我还想通过突出显示(选择)文本来直观地向用户显示此部分。
这里是一些示例代码:
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()
有没有简单的方法可以做到这一点?
最佳答案
尝试
entry.tag_add('sel', '1.0', 'end')
或
entry.tag_add('sel', '1.0', 'end-1c')
关于python - 如何通过单击按钮突出显示(选择)文本小部件中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24937858/