2011-03-08 67 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()