如何控制tkinter下拉框选中高亮显示

10 投票
3 回答
21870 浏览
提问于 2025-04-16 13:16

我写了一个小的法拉第转换器,目的是学习图形用户界面(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()

3 个回答

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

是不是说,使用只读的下拉框时,问题不在于选择,而是在于比较明显的焦点指示?

如果用这个方法,你就失去了用键盘控制程序的能力。要做到正确,你需要改变焦点高亮的样式。

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

你可以使用组合框(Combobox)里的 selection_clear() 方法来随时清除选中的内容。比如:

inUnitsValue.selection_clear()

撰写回答