2011-03-08 58 views
6

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

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

嗯,这里检查我的演示: http://stackoverflow.com/questions/18610519/ttk-combobox-glitch-when-state-is-read-only-and-out-of-focus – SzieberthAdam 2013-09-05 01:35:19

回答

7

您可以使用组合框的selection_clear()方法清除的选择,只要你想。 e.g

inUnitsValue.selection_clear() 
+0

我不想清除它,只要清楚突出显示,如果你知道我的意思。看看这个图片是怎么来的:http://i.imgur.com/SX1S2.png,组合框的选择是灰色的(在输入框中的“47”右边是“纳法(nF)”)?多数民众赞成在我想要的方式。它会随机切换到那个。 – jtkiv 2011-03-08 18:56:08

+0

@jtkiv:当我在此上下文中说'清除'时,我的意思是它删除选择突出显示。值保持不变。 – 2011-03-10 17:29:14

+0

啊。很棒!无论如何要清除所有选择? (现在程序比较大) – jtkiv 2011-03-10 22:02:04

5

难道是与只读组合框的问题不在于选择,但比较强烈关注,指标呢?

有了这个工作周,你失去了通过键盘控制你的程序的能力。要做到这一点,你必须改变焦点突出显示的风格。

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() 
相关问题