2011-03-08 180 views
6

Escribí un pequeño convertidor de faradio para aprender la programación de GUI. Funciona muy bien, se ve bien. El único problema es que no puedo entender cómo controlar este extraño resalte que aparece en mis selecciones ttk.Combobox. Usé un ttk.Style(), pero solo cambié los colores del fondo ttk.Combobox, entradas, etc. También traté de cambiar los temas openbox/gtk.Cómo controlar la selección de combobox tkinter resaltando

what the farad

Estoy hablando de lo que se ve allí en los "microfaradios (UF)" de texto.

Estaría bien, si resaltara toda la caja; pero prefiero que se haya ido por completo.

¿Cómo puedo manipular la selección de ttk.Combobox?

# what the farad? 
# thomas kirkpatrick (jtkiv) 

from tkinter import * 
from tkinter import ttk 

# ze la programma. 
def conversion(*args): 
# this is the numerical value 
inV = float(inValue.get()) 
# these two are the unit (farads, microfarads, etc.) values 
inU = inUnitsValue.current() 
outU = outUnitsValue.current() 

# "mltplr" is multiplied times inValue (inV) 
if inU == outU: 
    mltplr = 1 
else: 
    mltplr = 10**((outU - inU)*3) 
outValue.set(inV*mltplr) 

# start of GUI code 
root = Tk() 
root.title("What the Farad?") 

# frame 
mainFrame = ttk.Frame(root, width="364", padding="4 4 8 8") 
mainFrame.grid(column=0, row=0) 

# input entry 
inValue = StringVar() 
inValueEntry = ttk.Entry(mainFrame, width="20", justify="right", textvariable=inValue) 
inValueEntry.grid(column=1, row=1, sticky="W") 

# input unit combobox 
inUnitsValue = ttk.Combobox(mainFrame) 
inUnitsValue['values'] = ('kilofarads (kF)', 'farads (F)', 'millifarads (mF)', 'microfarads (uF)', 'nanofarads (nF)', 'picofarads (pF)') 
inUnitsValue.grid(column=2, row=1, sticky="e") 
inUnitsValue.state(['readonly']) 
inUnitsValue.bind('<<ComboboxSelected>>', conversion) 

# result label 
outValue = StringVar() 
resultLabel = ttk.Label(mainFrame, textvariable=outValue) 
resultLabel.grid(column=1, row=2, sticky="e") 

# output unit combobox 
outUnitsValue = ttk.Combobox(mainFrame) 
outUnitsValue['values'] = ('kilofarads (kF)', 'farads (F)', 'millifarads (mF)', 'microfarads (uF)', 'nanofarads (nF)', 'picofarads (pF)') 
outUnitsValue.grid(column=2, row=2, sticky="e") 
outUnitsValue.state(['readonly']) 
outUnitsValue.bind('<<ComboboxSelected>>', conversion) 

# padding for widgets 
for child in mainFrame.winfo_children(): child.grid_configure(padx=4, pady=4) 

# focus 
inValueEntry.focus() 

# bind keys to convert (auto-update, no button) 
root.bind('<KeyRelease>', conversion) 

root.mainloop() 
+2

Bueno, comprobar mi demo aquí: http://stackoverflow.com/questions/18610519/ttk-combobox-glitch-when-state-is-read-only-and-out-of-focus – SzieberthAdam

Respuesta

7

puede utilizar el método del cuadro combinado selection_clear() para borrar la selección siempre que lo desee. por ejemplo

inUnitsValue.selection_clear() 
+0

No quiero borrarlo, simplemente borre el resaltado si sabe a qué me refiero. ver cómo en esta foto: http://i.imgur.com/SX1S2.png, la selección del cuadro combinado es gris (los "nanofarads (nF)" a la derecha del "47" en el cuadro de entrada)? esa es la forma en que lo quiero cambiará aleatoriamente a eso. – jtkiv

+0

@jtkiv: cuando digo 'claro' en este contexto quiero decir que elimina el resaltado de selección. El valor permanece igual. –

+0

ah. ¡Funciona genial! de todos modos para borrar toda selección? (el programa es más grande ahora) – jtkiv

5

Podría ser que con un cuadro combinado de sólo lectura, el problema no es la selección, pero el relativamente fuerte enfoque en indicadores?

Con este workarround usted pierde la capacidad de controlar su programa por teclado. Para hacerlo bien, tendría que cambiar el estilo del enfoque de resaltado.

from tkinter import * 
from ttk import * 

def defocus(event): 
    event.widget.master.focus_set() 

root = Tk() 

comboBox = Combobox(root, state="readonly", values=("a", "b", "c")) 
comboBox.grid() 
comboBox.set("a") 
comboBox.bind("<FocusIn>", defocus) 

mainloop() 
Cuestiones relacionadas