Tkinter Menu.add_checkbutton()
Matthew Dixon Cowles
matt at mondoinfo.com
Mon Feb 19 22:57:27 EST 2001
On Tue, 20 Feb 2001 02:51:16 GMT, David Allen <s2mdalle at titan.vcu.edu>
wrote:
>Just a quick question:
>
>When I add a checkbutton to a menu:
[. . .]
>The checkbutton always displays as 'off' regardless of the value of
>'v'. See, depending on the value of 'v', I'd like to have it start
>as on if v is true.
I'm not sure what's going on with your code, but setting a checkbox to
on at the start works for me. One thing that I was confused by is
that the default action is to toggle the state of the checkbox: it's
not necessary to do that in a callback. But that's probably not the
problem that you're having. I'll append a minimal example that seems
to work for me.
BTW, Fredrik Lundh's excellent An Introduction to Tkinter is very
useful. I almost always have a copy open when I'm doing Tkinter
programming. It's at:
http://www.pythonware.com/library/an-introduction-to-tkinter.htm
And I find Pmw's menus easier to deal with than the native Tkinter
kind. Pmw is at:
http://pmw.sourceforge.net/
Regards
Matt
import sys
from Tkinter import *
class mainWin:
def __init__(self):
self.root=Tk()
self.toggleVar=IntVar()
self.toggleVar.set(1)
self.createWidgets()
return None
def createWidgets(self):
menuBar=Menu(self.root)
fileMenu=Menu(menuBar, tearoff=0)
fileMenu.add_checkbutton(label="Toggle",variable=self.toggleVar)
fileMenu.add_command(label="Quit", command=self.quitCB)
menuBar.add_cascade(label="File",menu=fileMenu)
self.root.config(menu=menuBar)
return None
def quitCB(self):
sys.exit(0)
def main():
mainWin()
mainloop()
if __name__== '__main__':
main()
More information about the Python-list
mailing list