在 python 的 tkinter 中,如何制作标签以便您可以用鼠标选择文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1602106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In python's tkinter, how can I make a Label such that you can select the text with the mouse?
提问by Ross Rogers
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?
在 python 的 tkinter 界面中,是否有一个配置选项可以更改 Label 以便您可以选择 Label 中的文本,然后将其复制到剪贴板?
EDIT:
编辑:
How would you modify this "hello world" app to provide such functionality?
您将如何修改这个“hello world”应用程序以提供此类功能?
from Tkinter import *
master = Tk()
w = Label(master, text="Hello, world!")
w.pack()
mainloop()
采纳答案by Bryan Oakley
The easiest way is to use a disabled text widget with a height of 1 line:
最简单的方法是使用高度为 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()
You could use an entry widget in a similar manner.
您可以以类似的方式使用条目小部件。
回答by Sunjay Varma
Made some changes to the above code:
对上面的代码做了一些修改:
from tkinter import *
master = Tk()
w = Text(master, height=1)
w.insert(1.0, "Hello, world!")
w.pack()
# 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(bg=master.cget('bg'), relief="flat")
w.configure(state="disabled")
mainloop()
The relief needs to be flat in order for it to look like an ordinary part of the display. :)
浮雕需要平整,才能使其看起来像显示器的普通部分。:)
回答by Jaidee
You can make texts which are selectable using either Text
or Entry
I really find both useful, using text can be really helpful! Here I show you a code of Entry:
您可以使用任一种来制作可选择的文本,Text
或者Entry
我真的觉得两者都很有用,使用文本真的很有帮助!在这里我给你看一个入口代码:
from tkinter import *
root = Tk()
data_string = StringVar()
data_string.set("Hello World! But, Wait!!! You Can Select Me :)")
ent = Entry(root,textvariable=data_string,fg="black",bg="white",bd=0,state="readonly")
ent.pack()
root.mainloop()
回答by Vignesh Arunachalam
Tried Bryan Oakley's answer. Able to select the text but couldn't copy it to clipboard. Here is a workaround.
尝试了 Bryan Oakley 的回答。能够选择文本但无法将其复制到剪贴板。这是一个解决方法。
from Tkinter import *
def focusText(event):
w.config(state='normal')
w.focus()
w.config(state='disabled')
master = Tk()
w = Text(master, height=1, borderwidth=0)
w.insert(1.0, "Hello, world!")
w.pack()
w.configure(state="disabled")
w.bind('<Button-1>', focusText)
mainloop()
We could not copy the text unless the widget gets focused. We are anyway gonna use the mouse button1 (left-click) to select the text, so binding it to a function which enables the text widget, sets focus on it, then disables it again.
除非小部件获得焦点,否则我们无法复制文本。无论如何,我们将使用鼠标 button1(左键单击)选择文本,因此将其绑定到启用文本小部件的函数,将焦点设置在它上面,然后再次禁用它。