using list elements in menu construction?
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@midwinter.ca stewart.midwinter@gmail.com
I don't see what's wrong with your program, but a simple Tkinter program not using pmw seems to work just as expected on my system. It seems like it would be hard for Python/PMW/Tkinter to even know that a particular option for varible= was computed by a list indexing operation, as opposed to any other way. Whatever problem is in your code, I think you may not have identified it correctly. Jeff #------------------------------------------------------------------------ import Tkinter # Create widgets t = Tkinter.Tk() m = Tkinter.Menu(t); t.configure(menu=m) menu = Tkinter.Menu(m); m.add_cascade(menu=menu, label="Radiobuttons") opts = [Tkinter.IntVar() for i in range(15)] # Callback when checkbutton clicked def print_opts(): for i in range(15): print "Checkbutton %d: %d" % (i, opts[i].get()) print # Populate the checkbutton menu for i in range(15): menu.add_checkbutton(variable=opts[i], label="Checkbutton %d" % i, command=print_opts) # Run the application t.mainloop() #------------------------------------------------------------------------
Jeff: you're right, there seems to be no problem with using list elements to construct menus IF you use basic Tkinter menus, and not Pmw menus (thanks for the example, BTW). I prefer using Pmw menus, so I'd like to get this to work, but OTOH it might be easier to use Tkinter menus, so that the end user doesn't have to have Pmw installed. BTW, I'm building a GUI front end for Vivian deSmedt's rsync.py, so my menu structure will be fairly simple. cheers, -- Stewart Midwinter stewart@midwinter.ca stewart.midwinter@gmail.com
On Tue, 1 Mar 2005 07:08:47 -0700 Stewart Midwinter <stewart.midwinter@gmail.com> wrote: Hi Stewart, there's a typo in your drawGui() method:
for i in range(0,15): print '%s: %i ' % (self.names[i], self.opts[i].get()) menuBar.addmenuitem('Options', 'checkbutton', self.names[i], ^^^^^^^
I guess you meant 'Edit', at least you have defined an 'Edit' menu and no 'Options' menu before. Best regards Michael
Doh! I guess it's true what they say, that you can't proof-read your own work.! thanks Michael. cheers S On Tue, 1 Mar 2005 20:42:58 +0100, Michael Lange <klappnase@web.de> wrote:
there's a typo in your drawGui() method:
-- Stewart Midwinter stewart@midwinter.ca stewart.midwinter@gmail.com
participants (3)
-
Jeff Epler -
Michael Lange -
Stewart Midwinter