2013-11-22 76 views
0

我已經嘗試了幾個examples from stackoverflow,但不幸的是我不工作。Tkinter得到一個CheckButton的選定值

我只想得到選定的Tkinter,Python的checkButton的值。

我有CheckButton名單及其類似下面

## csv file has rows like 
# 101, apple 
# 102, orange 
for row in csvReader: 
     checkButton = Checkbutton(top, text = row[1], variable = StringVar(), 
       onvalue = row[0], offvalue = "0", height=2, \ 
       width = 0, justify=Tkinter.LEFT) 
     checkButton.pack() 
     checkBoxList.append(checkButton) 

在從表單點擊一個按鈕,在這裏是需要抓住複選框的選中的值回調。

def btnStartCallBack(): 
    for chkBox in checkBoxList: 
     print chkBox.variable().get() 
     # also tried below 
     # print chkBox.get() 
     # print chkBox.var() 
     # print chkBox.onvalue.get() 

返回:

AttributeError: Checkbutton instance has no attribute 'variable' 

我只是想知道是否有可能或不被選擇時,他們得到CheckButton的價值。而且我應該尋找這個屬性?

回答

1

我通常在一堂課上做我的圖形用戶界面,比如http://zetcode.com/。我會做這樣的事情

self.v = StringVar() 
self.cb1 = CheckButton(self, text=row[1], variable=self.v) 

再後來就..

self.v.get() 

我認爲你可能需要在你的代碼不同的聲明variable。 Bon chasse!

+0

是的,你是對的。其也相似。我必須以不同的方式聲明變量。 ** CheckVar1 = StringVar()**和** Checkvar1.get()**返回值。輕鬆...謝謝m8 ... –

+0

沒問題。我記得我第一次知道這些東西 - 我不知道我必須使用'get()',並且花了我數小時才弄明白。 – cjohnson318

相關問題