簡體   English   中英

Tkinter獲取CheckButton的選定值

[英]Tkinter get selected value of a CheckButton

我已經嘗試過stackoverflow中的幾個例子 ,但不幸的是對我不起作用。

我只想獲得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時是否有可能獲得CheckButton的值。 還有哪個屬性我應該尋找這個?

我經常在課堂上做我的GUI,比如http://zetcode.com/ 我會做點什么的

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

然后......

self.v.get()

我認為您可能需要在代碼中以不同方式聲明variable Bon chasse!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM