【问题标题】:How do I highlight (select) text in Text widget with a button click?如何通过单击按钮突出显示(选择)文本小部件中的文本?
【发布时间】:2014-07-24 15:24:24
【问题描述】:

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

有没有简单的方法来做到这一点?

【问题讨论】:

    标签: python text tkinter widget


    【解决方案1】:

    试试

    entry.tag_add('sel', '1.0', 'end')
    

    entry.tag_add('sel', '1.0', 'end-1c')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2017-11-18
      • 1970-01-01
      • 2012-10-01
      相关资源
      最近更新 更多