在python的tkinter中,如何制作标签,以便可以用鼠标选择文本?


问题内容

在python的tkinter界面中,是否有一个配置选项可以更改Label,以便您可以选择Label中的文本,然后将其复制到剪贴板?

编辑:

您将如何修改此“ hello world”应用程序以提供此类功能?

from Tkinter import *

master = Tk()

w = Label(master, text="Hello, world!")
w.pack()

mainloop()

问题答案:

最简单的方法是使用高度为1行的禁用的文本小部件:

from Tkinter import *

master = Tk()

w = Text(master, height=1, borderwidth=0)
w.insert(1.0, "Hello, world!")
w.pack()

w.configure(state="disabled")

# if tkinter is 8.5 or above you'll want the selection background
# to appear like it does when the widget is activated
# comment this out for older versions of Tkinter
w.configure(inactiveselectbackground=w.cget("selectbackground"))

mainloop()

您可以以类似方式使用条目窗口小部件。