CheckButton -- modify "check state"

Eric Brunel eric.brunel at
Thu Jan 15 09:50:56 CET 2004

Askari wrote:
> Hi,
>    How do for do a "select()" on a CheckButton in a menu (make with
> add_checkbutton(....) )?
> I can modify title, state, etc but not the "check state". :-(

Assuming you're using Tkinter, the way to do it is to use the variable option 
when you create the menu, and them modify the variable. Here is an example code:
from Tkinter import *

root = Tk()

## Create the menu bar
menuBar = Menu(root)

## Create the menu
menu = Menu(menuBar)
menuBar.add_cascade(label='Menu', menu=menu)

## Create the variable for the check button
myCheckVar = BooleanVar()

## Functions to check/uncheck/print the check state
def check(*whatever): myCheckVar.set(1)
def uncheck(*whatever): myCheckVar.set(0)
def print_option(*whatever): print myCheckVar.get()

## Entries in menu
menu.add_checkbutton(label='Option', variable=myCheckVar)
menu.add_command(label='Check', command=check)
menu.add_command(label='Uncheck', command=uncheck)
menu.add_command(label='Print', command=print_option)


If you do not specify a command for the add_checkbutton, the default action is 
to toggle the check state.

- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools -

More information about the Python-list mailing list