Tkinter checkbutton menu item,

Martin Franklin martin.franklin at waii.com
Fri Aug 25 04:59:26 EDT 2000


I finally got it to NOT work!  and worked out how to fix the problem
at the same time.  Basically it depends what toplevel window I was using
If I use a Toplevel() then checkbutton menu items do not work correctly
however if I use Tk() as my toplevel then all is well. I attached two
modules
to better explain the problem if anyone is interseted.  


Thanks,
Martin.


Martin Franklin wrote:
> 
> Well, thanks Richard, but I atempted to re-write just the 'problem code'
> to send in ( the original is a few hundred lines) and guess what...  I
> can't reproduce the problem!  Looks like I need to take the original
> code apart bit by bit and take a good look at it!
> 
> Richard Chamberlain wrote:
> >
> > Hi Martin,
> >
> > Can you show this in a small amount of code.
> >
> > Thanks
> >
> > Richard
> >
> > Martin Franklin wrote:
> > >
> > > Hi,
> > >
> > > I have a problem with my checkbutton menu items.....
> > > I have a `main` python program that calls in a new window
> > > and displays a canvas with some graphs on....  This new window
> > > has it's own menu bar and a couple of menu items are check buttons.
> > >
> > > Ok so far.
> > >
> > > When I destroy this new window and call it up again from my main program
> > > the checkbuttons that I changed in the first instance 'remember' they
> > > were checked and look reversed if you know what I mean.  This
> > > new window is in it's own class in a seperate module.  I have tried to
> > > reload the module but still it remembers...
> > >
> > >
> > > Q.  Why....how...
> > >
> > > TIA
> > > Martin.
> > >
> > > BTW i'm using 1.5.2 on 3xUnix, RH Linux and Win*
> > --
> > http://www.python.org/mailman/listinfo/python-list
> 
> --
>           Martin Franklin Workstation Support
>       (Tel. Work 01234 224793, Home 01234 881722)
> Western G., Manton Lane, Bedford, Bedfordshire, MK41 7PA
>            martin.franklin at bedford.waii.com
> 
> --
> http://www.python.org/mailman/listinfo/python-list

-- 
          Martin Franklin Workstation Support
      (Tel. Work 01234 224793, Home 01234 881722)
Western G., Manton Lane, Bedford, Bedfordshire, MK41 7PA
           martin.franklin at bedford.waii.com
-------------- next part --------------
from Tkinter import *


class Main:
    def __init__(self):


        root = Tk()
        f = Frame(root)

        b = Button(f, text='Press me', command=self.import_module)
        b.pack()
        b = Button(f, text='Exit', command=root.quit)
        b.pack()
        f.pack()

        root.mainloop()

    def import_module(self):
        exec 'import ' + 'menu_module'
        module = eval('menu_module')
        mm = module.main()
        return_code = mm.first()

Main()
-------------- next part --------------

from Tkinter import *



class main:
    def callback1(self):
        if self.cb1==0:
            self.cb1=1
        else:
            self.cb1=0
        self.print_callback()

                    
    def callback2(self):
        if self.cb2==0:
            self.cb2=1
        else:
            self.cb2=0
        self.print_callback()
            
        
    def print_callback(self):
        print 'callback 1 =%i' %self.cb1
        print 'callback 2 =%i' %self.cb2

            
    
    def first(self):
        top = Toplevel()
        self.cb1=0
        self.cb2=0
        frame = Frame(top,relief='raised', bd=2)
        frame.pack(fill='x')
        cmenu = Menubutton(frame, text='Color', underline=0)
        cmenu.pack(side='left', padx='2m', anchor='nw')
        cmenu.menu = Menu(cmenu)
        cmenu.menu.add_checkbutton(label='Check 1', command=self.callback1)
        cmenu.menu.add_checkbutton(label='Check 2', command=self.callback2)
        cmenu.menu.add_command(label='Exit', command=top.quit)
        cmenu['menu'] = cmenu.menu
        
        cmenu.menu.invoke(cmenu.menu.index('Check 2'))
        
        top.mainloop()
        top.withdraw()
        
        


More information about the Python-list mailing list