I have some Tkinter code with a Label object. The user needs to be able to select text from the Label in order to copy the text (and paste into something else). However, it seems Labels aren't selectable? I can't figure out how to enable this... any suggestions?

from Tkinter import *

master = Tk()

label = Label(master, text="Example: foo_bar(1,2,3)")
label.pack()

mainloop()

I googled this from http://bytes.com/topic/python/answers/642659-tkinter-label-widget-text-selection:

import Tkinter as Tk
root = Tk.Tk()

ent = Tk.Entry(root, state='readonly', readonlybackground='white', fg='black')
var = Tk.StringVar()
var.set('Some text')
ent.config(textvariable=var, relief='flat')
ent.pack()
root.mainloop()

That sort of works, however when you select the text, it's clear that its a text entry field instead of a label. This is because a thick border appears around the text box when you click on it. Is there any way to not have the border appear when selected?

Hi, your code works perfectly fine:

import Tkinter as Tk
root = Tk.Tk()
ent = Tk.Entry(root, state='readonly', readonlybackground='white', fg='black')
var = Tk.StringVar()
var.set('Some text')
ent.config(textvariable=var, relief='flat')
ent.pack()
root.mainloop()

But instead of 'Some text' I would like to put a variable of an entry, to display what the user types, but that you are able to copy paste it! For now my code is:

from Tkinter import *

frame = Tk()

userinput = StringVar()

entry = Entry(frame, textvariable=userinput)

userinput.get()

entry2 = Entry(frame, state="readonly")

entry2.set(userinput)

entry2.pack()
entry.pack()

frame.mainloop()

It displays an error, and I have to figure this out soon, for a school project
Hope you can help :D Thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.