Sabuj Hassan
Sabuj Hassan

Reputation: 39443

Tkinter get selected value of a CheckButton

I have tried several examples from stackoverflow, but didn't work for me unfortunately.

I just want to get the value of selected checkButton of Tkinter, Python.

I have a list of CheckButton and its like below

## 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)

On clicking a button from the form, here is the callback that needs to grab the checked value of checkbox.

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

it returns:

AttributeError: Checkbutton instance has no attribute 'variable'

I just want to know whether it is possible or not to get the value of CheckButton when they are selected. And also at which attribute should I look for this?

Upvotes: 0

Views: 16676

Answers (1)

cjohnson318
cjohnson318

Reputation: 3253

I usually do my GUIs up in a class, like http://zetcode.com/. I'll do something like

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

and then later on..

self.v.get()

I think that you might need to declare variable differently in your code. Bon chasse!

Upvotes: 5

Related Questions