In python's tkinter, how can I make a Label such that you can select the text with the mouse?

Question:

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?

EDIT:

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()
Asked By: Ross Rogers

||

Answers:

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.

Answered By: Bryan Oakley

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. 🙂

Answered By: Sunjay Varma

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:

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()
Answered By: Jaidee

Tried Bryan Oakley’s answer. Able to select the text but couldn’t copy it to clipboard. Here is a workaround.

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.

Answered By: Vignesh Arunachalam

The other answers insert text to the text box instead of replacing the text. That works when you need to change the text only for one time. However, you need to delete the line first if you need to replace it. The following code would fix this issue:

from tkinter import *

master = Tk()

w = Text(master, height=1)
w.delete(1.0, "end")
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()
Answered By: AKMalkadi

OP asked how to make text in a Label widget select-able, and all of the responses (with mad respect as due to Bryan Oakley) instructed OP instead to create a non-Label widget.

Label widgets are easier to work with on the development side because their text is rewritable with a simple .config(text="") method. The delete and insert methods of the Text widget get really complicated for updating portions of the widget’s text, and there is additional work needed to deal with the widget’s likely read-only state. The textvariable attribute of the Entry widget raises its own complexities in terms of namespace adjustment and maintenance. Of course these complications are manageable, but they do require that additional effort to manage them, and we’re here in Python exactly because we want things done as cleanly as possible, right?

Assuming the unstated (vulgar) answer to OP’s question is "You can’t", I’ll ask the follow-up question:

"How would one modify the Tkinter package itself to enable text-selection in the label widget?"

Answered By: M.Argyle.22
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.