2009-10-21 25 views
5

En la interfaz tkinter de python, ¿hay alguna opción de configuración que cambie una etiqueta para que pueda seleccionar el texto en la etiqueta y luego copiarla en el portapapeles?En tkinter de python, ¿cómo puedo hacer una etiqueta para que pueda seleccionar el texto con el mouse?

EDIT:

¿Cómo modificar esta "hola mundo" de la aplicación para proporcionar dicha funcionalidad?

from Tkinter import * 

master = Tk() 

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

mainloop() 

Respuesta

9

La forma más sencilla es utilizar un widget de texto de lesionados con una altura de 1 línea:

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

Se podría utilizar un control de entrada de una manera similar.

+1

Para mí, 'state =" disabled "' ni siquiera me deja seleccionar texto para copiar. Poniéndolo en 'state =" readonly "' realmente funcionó. – AneesAhmed777

4

hecho algunos cambios en el código anterior:

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

El relieve tiene que ser plana con el fin de que se vea como una parte normal de la pantalla. :)

Cuestiones relacionadas