[Tutor] Pmw/Tkinter question

Albert-Jan Roskam sjeik_appie at hotmail.com
Sat Mar 12 03:17:11 EST 2016


Hello,

Below is a slightly modified example about Pmw/Tkinter from http://pmw.sourceforge.net/doc/howtouse.html.
I changed the geometry manager from 'pack' to 'grid', because I use that in all other parts of my program.
The problem is, 'broccoli' won't display nicely under vege_menu. I want it to left-align, not center (I don't case that it's truncated). In plain Tkinter I could do "self.vege_menu.configure(anchor=Tkinter.W)" but this raises a KeyError in Pmw.

The real reason for switching from Tkinter to Pmw is that Pmw is more compact, and it has an 'index' method that I could use to link two optionmenus.

Any ideas? Hope this is ok to ask as Pmw is a layer on top of Tkinter.

Thank you!
Albert-Jan


# -*- coding: utf-8 -*-

import Tkinter
import Pmw

class Demo:
    def __init__(self, parent):
        # Create and pack the OptionMenu megawidgets.
        # The first one has a textvariable.
        self.var = Tkinter.StringVar()
        self.var.set('steamed')
        self.method_menu = Pmw.OptionMenu(parent,
                labelpos = 'w',
                label_text = 'Choose method:',
                menubutton_textvariable = self.var,
                items = ['baked', 'steamed', 'stir fried', 'boiled', 'raw'],
                menubutton_width = 10,
        )
        #self.method_menu.pack(anchor = 'w', padx = 10, pady = 10)
        self.method_menu.grid(row=0, column=0, sticky = 'w', padx = 10, pady = 10)

        self.vege_menu = Pmw.OptionMenu (parent,
                labelpos = 'w',
                label_text = 'Choose vegetable:',
                items = ('broccoli-broccoli-broccoli-broccoli', 'peas', 'carrots', 'pumpkin'),
                menubutton_width = 10,
                command = self._printOrder,
        )
        #self.vege_menu.pack(anchor = 'w', padx = 10, pady = 10)
        self.vege_menu.grid(row=1, column=0, sticky = 'w', padx = 10, pady = 10)

        self.direction_menu = Pmw.OptionMenu (parent,
                labelpos = 'w',
                label_text = 'Menu direction:',
                items = ('flush', 'above', 'below', 'left', 'right'),
                menubutton_width = 10,
                command = self._changeDirection,
        )
        #self.direction_menu.pack(anchor = 'w', padx = 10, pady = 10)
        self.direction_menu.grid(row=2, column=0, sticky = 'w', padx = 10, pady = 10)

        menus = (self.method_menu, self.vege_menu, self.direction_menu)
        Pmw.alignlabels(menus)

    def _printOrder(self, vege):
        # Can use 'self.var.get()' instead of 'getcurselection()'.
        print 'You have chosen %s %s.' % \
            (self.method_menu.getcurselection(), vege)

    def _changeDirection(self, direction):
        for menu in (self.method_menu, self.vege_menu, self.direction_menu):
            menu.configure(menubutton_direction = direction)
            
if __name__ == "__main__" :
    root = Pmw.initialise()
    root.title("Demo")
    widget = Demo(root)
    root.mainloop() 		 	   		  


More information about the Tutor mailing list