0

I create Checkbutton

var = IntVar()
cb = Checkbutton(master, variable=var)

I can set and read status checkbutton with variable

var.set(1)
var.get()

I can set the status of a checkbutton via the checkbutton itself

cb.select()
cb.deselect()

How can I read the status of a checkbutton through a variable cb without variable var?

Arty
  • 579
  • 1
  • 8
  • 17

1 Answers1

1

You have to use a Tkinter variable to query the state of the Checkbutton. It is also a very common pattern, so you shouldn't worry about the extra line of code used to define the IntVar. It has also its advantages, like the possibility to trace when the value of the variable changes.

A. Rodas
  • 20,171
  • 8
  • 62
  • 72
  • I know how to use a variable. I do not need to follow when the value of the variable changes. I control the visibility of the widget. Why do I need two variables when one is enough? – Arty Jun 23 '13 at 14:42
  • @Arty There aren't two variables, just one: The current state of the Checkbutton is not stored in any attribute, so even though it is possible to select or deselect, you don't have any other method to retrieve the value. – A. Rodas Jun 23 '13 at 15:24