python - 如何通过单击按钮突出显示(选择)文本小部件中的文本?

标签 python text tkinter widget

我正在使用 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/

相关文章:

c# - 如何从 Xamarin 使用 Python

python - 无法在 Python 控制台中使用 'cd' 命令

c - 分割从文本文件中读取的长句子

regex - R- 文本值连续显示多少次?

python - 使单元格适合文本(并使其只读)

Python TKinter 文本变量

python - heroku 服务器上的 django makemigrations 和 migrate 不创建表

python - 在 Python 和 Perl 套接字之间交换数据

html - 如何放大此文本的垂直区域并减去其水平区域?

python - 在 Ubuntu 上找不到 Tkinter 模块