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

2024-04-14

我写了一个小型法拉转换器来学习 GUI 编程。它效果很好,看起来不错。唯一的问题是我似乎不知道如何控制我的屏幕上出现的这种奇怪的突出显示ttk.Combobox选择。我确实用过ttk.Style(),但它只改变了颜色ttk.Combobox背景,条目等我也尝试改变openbox/gtk themes.

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

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

使用此解决方法,您将失去通过键盘控制程序的能力。为了正确执行此操作,您必须更改焦点突出显示的样式。

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何控制 tkinter 组合框选择突出显示 的相关文章

随机推荐