2012-03-04 58 views
8

Estoy tratando de cambiar el color de fondo por defecto para el texto seleccionado en un widget de texto Tkinter en Mac OS X cuando el widget no tiene el foco. El color de selección desenfocado predeterminado es gris. Después de muchas horas de búsqueda, no pude encontrar una solución lista para usar para hacer esto. Esto es lo que he intentado:Tkinter cambiar el color de fondo en seleccione un widget de texto fuera de foco

  • Cambiar el color de selección con la opción selectbackground no cambia el color de selección cuando el widget no está enfocado. P.ej. Se mantiene gris.
  • Tampoco Text.tag_configure("sel", background=...)
  • Usando ttk.Style.map con las obras estatales "!focus" en los widgets de entrada (y otros), pero no al texto widgets.

Así que tuve que rodar el mío (ver abajo). ¿Hay una mejor manera de hacer esto?

import Tkinter as tk 

# Replace 'tag_out' with 'tag_in' 
def replace_tag(widget, tag_out, tag_in): 
    ranges = widget.tag_ranges(tag_out) 
    widget.tag_remove(tag_out, ranges[0], ranges[1]) 
    widget.tag_add(tag_in, ranges[0], ranges[1]) 

def focusin(e): 
    replace_tag(e.widget, "sel_focusout", "sel") 

def focusout(e): 
    replace_tag(e.widget, "sel", "sel_focusout") 


root = tk.Tk() 

# Create a Text widget with a red selected text background 
text = tk.Text(root, selectbackground="red") 
text.pack() 

# Add some text, and select it 
text.insert("1.0", "Hello, world!") 
text.tag_add("sel", "1.0", "end") 

# Create a new tag to handle changing the background color on selected text 
# when the Text widget loses focus 
text.tag_configure("sel_focusout", background="green") 
replace_tag(text, "sel", "sel_focusout") 

# Bind the events to make this magic happen 
text.bind("<FocusIn>", focusin) 
text.bind("<FocusOut>", focusout) 


# Create an Entry widget to easily test the focus behavior 
entry = tk.Entry(root) 
entry.pack() 

entry.insert("0", "Focus me!") 

root.mainloop() 
+0

Windows tiene el problema opuesto; por defecto, no hay fondo de selección inactivo. Para que también permanezca gris en Windows, Idle tiene una solución alternativa para este 'error' similar al tuyo. Alguien recientemente señaló 'inactiveselectbackground', así que estoy eliminando la solución alternativa hoy (desearía haber buscado SO y haber encontrado esto hace un año). La opción está documentada en https://www.tcl.tk/man/tcl8.5/TkCmd/text.htm#M-inactiveselectbackground. Estoy considerando trabajar en expandir los documentos incompletos de tkinter. –

Respuesta

11

Excavando a través del código fuente de Tk me llevan a la respuesta! La opción inactiveselectbackground establece el color.

import Tkinter as tk 

root = tk.Tk() 

# Create a Text widget with a red selected text background 
# And green selected text background when not focused 
text = tk.Text(root, selectbackground="red", inactiveselectbackground="green") 
text.pack() 

# Add some text, and select it 
text.insert("1.0", "Hello, world!") 
text.tag_add("sel", "1.0", "end") 

# Create an Entry widget to easily test the focus behavior 
entry = tk.Entry(root) 
entry.pack() 

entry.insert("0", "Focus me!") 

root.mainloop() 
Cuestiones relacionadas