如何控制tkinter组合框选择突出显示

jtk*_*kiv 9 python combobox tkinter ttk python-3.x

我写了一个小法拉转换器来学习GUI编程.它很棒,看起来很精致.唯一的问题是我似乎无法弄清楚如何控制我的ttk.Combobox选择中出现的这种奇怪的突出显示.我确实使用过ttk.Style(),但它只改变了ttk.Combobox背景,条目等的颜色.我也试过改变openbox/gtk主题.

法拉德

我在谈论文字"microfarads(uF)"中的内容.

如果突出显示整个盒子,那就没关系了.但我宁愿让它完全消失.

我怎样才能操纵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()
Run Code Online (Sandbox Code Playgroud)

Bry*_*ley 8

您可以使用Combobox的selection_clear()方法随时清除选择.例如

inUnitsValue.selection_clear()
Run Code Online (Sandbox Code Playgroud)


Gon*_*nzo 6

可能是因为只有一个只读组合框,问题不是选择而是相对强大的焦点指示器?

通过这种工作环境,您将失去通过键盘控制程序的能力.要做到正确,你必须改变焦点突出显示的风格.

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()
Run Code Online (Sandbox Code Playgroud)