[Tkinter-discuss] using list elements in menu construction?

Stewart Midwinter stewart.midwinter at gmail.com
Tue Mar 1 15:08:47 CET 2005


Hi all:

I want to use a list to contain variables in a series of option-menu
toggles.  I've got 15 options to set, so I would like to use a single
for loop to draw the 15 menu items and use a list containing Tkinter
IntVar instances to keep track of the value of these options.  But,
Tkinter doesn't seem to like me using a list member as the variable in
the menu.  Is there a way to do what I want?

Here's a sample app with the offending behaviour:

#test-opts.py
import sys, thread, string, os, time

class rsyncApp:
    def __init__(self, parent):
        '''define rsync defaults; get from settings file if it exists'''
        self.font = ('Helvetica', 10)	#default font
        os.chdir(folder)

    def drawGUI(self, parent):
        '''Create and pack the MenuBar.'''
        menuBar = Pmw.MenuBar(parent,
            hull_relief = 'raised',
            hull_borderwidth = 1)
        menuBar.pack(fill = 'x')
        self.menuBar = menuBar
    
        # Add some buttons to the MenuBar.
        menuBar.addmenu('File', 'Connect or quit')
        # File menu item 0
        menuBar.addmenuitem('File', 'command', 'Exit the application',
           command = self.quit,
           font = self.font,
           label = 'Exit')
    
        menuBar.addmenu('Edit', 'Edit Options')

        self.names =  ["Quiet","Recursive",
                    "Relative path names","Update only",
                "Preserve times","Dry run",
                    "only update Existing","Delete missing source",
                "Delete excluded on dest",
                    "Delete excluded from source","ignore times",
                "Use size only", "CVS-exclude",
                    "print version (ignore other options)","help
(ignore other options)",]

        self.opts = []
        for i in range(0,15):
            self.opts.append(None)
        for i in range(0,15):
        # Create a checkbutton menu item.
            self.opts[i] = Tkinter.IntVar()
            # Initialise the checkbutton to 1:
            self.opts[i].set(0)
        print "Options:", self.opts
        for i in range(0,15):
            print '%s: %i ' % (self.names[i], self.opts[i].get())
            menuBar.addmenuitem('Options', 'checkbutton', self.names[i],
                    label = self.names[i],
                    font = self.font,
                    command = self._toggleMe,
                    variable = self.opts[i])
            self._toggleMe()
    
    def _toggleMe(self):
        '''get the option settings'''
        for i in range(0,15):
            print '%s value:' %self.names[i], self.opts[i].get()

    def quit(self):
        '''clean up nicely when we quit'''
        root.destroy()
        root.quit()

if __name__ == '__main__':
    
    OS = os.name.lower()
    if OS == 'ce':
        import osce
        import pythonrc     #contains the sys.path.append() commands
        folder = '\\My Documents\\Python'
    else:
        # we are on the desktop
        sys.path.append('c:\\programs')
        folder = os.getcwd()
    import Tkinter, Pmw
    from tkMessageBox import *

    root = Tkinter.Tk()

    #initialise global vars

    global sizex, sizey, startx, starty
    if OS == 'ce':
        sizex, sizey = root.wm_maxsize()
        startx = 0; starty = 0
        sizex = sizex - startx -6
        sizey = sizey - starty -6
    else:
        xmax, ymax = root.wm_maxsize()
        sizex, sizey = (240, 400)
        startx = xmax/4; starty = ymax/4
        
    Pmw.initialise(root)
    root.title('rsync')
    global geometry
    geometry = "%dx%d+%d+%d"%(sizex,sizey,startx,starty)
    root.wm_geometry(geometry)
    root.update()
    app = rsyncApp(root)
    app.drawGUI(root)
    root.mainloop()

thanks,
-- 
Stewart Midwinter
stewart at midwinter.ca
stewart.midwinter at gmail.com


More information about the Tkinter-discuss mailing list