【问题标题】:How to control the tkinter combobox selection highlighting如何控制 tkinter 组合框选择突出显示
【发布时间】:2011-03-08 17:23:08
【问题描述】:

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

我说的是文本“微法拉 (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()

【问题讨论】:

标签: python combobox python-3.x tkinter ttk


【解决方案1】:

您可以随时使用组合框的selection_clear() 方法清除选择。 例如

inUnitsValue.selection_clear()

【讨论】:

  • 我不想清除它,如果您知道我的意思,请清除突出显示。看看这张图片如何:i.imgur.com/SX1S2.png,组合框选择是灰色的(输入框中“47”右侧的“纳法拉(nF)”)?这就是我想要的方式。它会随机切换到那个。
  • @jtkiv:当我在这种情况下说“清除”时,我的意思是它删除了选择突出显示。值保持不变。
  • 啊。效果很好!无论如何要清除所有选择? (程序现在更大了)
【解决方案2】:

难道对于只读组合框,问题不在于选择,而是相对较强的焦点指示器?

使用此解决方法,您将失去通过键盘控制程序的能力。要做到这一点,您必须更改焦点突出显示的样式。

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

【讨论】:

    【解决方案3】:

    只需刷新 Combobox 的选定值。 这将有助于消除突出显示。

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

    【讨论】:

      猜你喜欢
      • 2022-07-01
      • 2012-04-20
      • 1970-01-01
      • 2017-06-12
      • 2015-12-28
      • 2010-12-07
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多