在 python 的 tkinter 中,如何制作标签以便您可以用鼠标选择文本? [英] In python's tkinter, how can I make a Label such that you can select the text with the mouse?

查看:34
本文介绍了在 python 的 tkinter 中,如何制作标签以便您可以用鼠标选择文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python的tkinter界面中,是否有一个配置选项会改变一个标签,这样你就可以选择标签中的文本,然后将其复制到剪贴板?

In python's tkinter interface, is there a configuration option that will change a Label such that you can select the text in the Label and then copy it to the clipboard?

你会如何修改这个hello world"应用来提供这样的功能?

How would you modify this "hello world" app to provide such functionality?

from Tkinter import *

master = Tk()

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

mainloop()

推荐答案

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

The easiest way is to use a disabled text widget with a height of 1 line:

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()

您可以以类似的方式使用条目小部件.

You could use an entry widget in a similar manner.

这篇关于在 python 的 tkinter 中,如何制作标签以便您可以用鼠标选择文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆