如何控制tkinter组合框的选中高亮显示

10
我写了一个小型法拉德转换器来学习GUI编程。它工作得很好,看起来还不错。唯一的问题是,我似乎无法控制在我的ttk.Combobox选择中出现的奇怪高亮显示。我确实使用了ttk.Style(),但它只改变了ttk.Combobox背景、输入等的颜色。我还尝试更改openbox/gtk主题。

what the farad

我在谈论文本中看到的"微法(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()

2
好的,请在这里查看我的演示: https://dev59.com/IHbZa4cB1Zd3GeqPF3S4 - SzieberthAdam
3个回答

8
您可以使用Combobox的selection_clear()方法来清除选择,无论何时您需要。例如:
inUnitsValue.selection_clear()

我不想清除它,只清除高亮显示(如果你知道我的意思)。看看这张图片:http://i.imgur.com/SX1S2.png,在这里,组合框选择是灰色的(在输入框中“47”右侧的“纳诺法(nF)”)?那就是我想要的方式。它会随机切换到那种方式。 - jtkiv
@jtkiv:在这个上下文中,当我说“清除”时,我指的是它会移除选择高亮显示,但值保持不变。 - Bryan Oakley
啊,很棒!有没有清除所有选择的方法?(程序现在更大了) - jtkiv

8

readonly combobox的问题可能并不是选择的问题,而是相对强烈的焦点指示器吗?

通过此解决方法,您会失去通过键盘控制程序的能力。为了正确处理这个问题,您需要改变焦点高亮的样式。

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

0
只需刷新组合框的选定值即可。这将有助于消除高亮显示。
import tkinter.ttk
import tkinter

items = ["test1","test2","test3","test4"]

class TkCombobox(tkinter.ttk.Combobox):
   def __init__(self, *arg, **kwarg):
      super(TkCombobox, self).__init__(*arg, **kwarg)
      self._strvar_ = tkinter.StringVar()
      self._strvar_.set("")
      self["textvariable"] = self._strvar_

      self.bind("<<ComboboxSelected>>", self.highlight_clear)

   def highlight_clear(self, event):
      current = self._strvar_.get()
      self.set("")
      self.set(current)

master = tkinter.Tk();master.geometry("400x400")
c = TkCombobox(master, values=items, state="readonly")
c.pack()

master.mainloop()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接