How to get a menu checkbutton to call a function in Python + Tkinter? How to get a menu checkbutton to call a function in Python + Tkinter? tkinter tkinter

How to get a menu checkbutton to call a function in Python + Tkinter?


Checkbutton Menu can definitely call a function. That is what the checkbutton option is there for :)

I generally use 2 functions to handle a call from the checkbutton menu.

first create that menu and assign it to a boolean var

yesno = BooleanVar(root)mymenu.add_checkbutton(label="Do this ?", variable=yesno, command=mytoggle)

then you need 2 functions:1) one callback to toggle 2) one to handle yes

def mytoggle(event=None):    val = yesno.get()    if val:        dosomething()    else:        somethingelse()


To answer your specific question, yes, the function will be called when you click on the checkbutton.

You should be calling add_command rather than add_checkbutton on the menu. Using a checkbutton to call a function from a menu is highly unusual and will likely confuse your users.