如何控制tkinter组合框选择高亮 [英] How to control the tkinter combobox selection highlighting

查看:582
本文介绍了如何控制tkinter组合框选择高亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小法拉第转换器来学习GUI编程。它工作伟大,看起来很好。唯一的问题是我似乎不知道如何控制这个奇怪的突出出现在我的 ttk.Combobox 选择。我使用了 ttk.Style(),但它只改变了 ttk.Combobox 背景的颜色,我也尝试改变 openbox / gtk 主题。

I wrote a small farad converter to learn GUI programming. It works great, looks fine-ish. The only problem is I can't seem to figure out how to control this strange highlighting that comes up on my ttk.Combobox selections. I did use a ttk.Style(), but it only changed the colors of the ttk.Combobox background, entries, etc. I also tried changing openbox/gtk themes.

我在说的是文本上的内容microfarads 。

I'm talking about what's seen there on the text "microfarads (uF)".

如果突出显示整个框,

It'd be fine, if it highlighted the entire box; but I'd rather have it gone completely.

如何操作 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()


推荐答案

您可以使用Combobox的 selection_clear() 方法来清除选择。

You can use the Combobox's selection_clear() method to clear the selection whenever you want. e.g

inUnitsValue.selection_clear()

这篇关于如何控制tkinter组合框选择高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆