[Tutor] Pmw/Tkinter question

boB Stepp robertvstepp at gmail.com
Sun Mar 13 00:08:20 EST 2016


On Sat, Mar 12, 2016 at 2:17 AM, Albert-Jan Roskam
<sjeik_appie at hotmail.com> wrote:
> 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)...

Not having run your code (I did not have Pmw installed.), I think I
misunderstood your question(s).  I went ahead and installed Pmw and
ran your code.  I don't have Py 2 installed, so I had to do the usual
conversions to make it Py 3 compatible.  Once I did that I got all of
your labels showing up on the right side of the menu buttons.  I
presume you want them on the left side of each button?  To do that you
with the grid manager, you do not want the buttons to attach to the
left side of the overall megawidget.  Instead, allow the menu buttons
to center in each row by omitting the sticky option from grid() and
then your labels have room to attach on the left side.  Assuming that
I am now trying to solve the correct problem, my Py *3* code is:

# -*- 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.grid(row=0, column=0, padx = 10, pady = 10)

        self.vege_menu = Pmw.OptionMenu (parent,
                labelpos = 'w',
                label_text = 'Choose vegetable:',
                items = ('broccoli', 'peas','carrots', 'pumpkin'),
                menubutton_width = 10,
                command = self._printOrder,
        )
        self.vege_menu.grid(row=1, column=0, 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.grid(row=2, column=0, 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()

I omitted the extra "broccoli" words, to clarify the alignment issues.
I hope I am going in the direction you wanted!  Nonetheless, having
never used the Python megawidgets before, I found this educational.
In passing, I was surprised that it does not matter about the case of
the sticky positions.  I know I have gotten errors at work by not
having the proper case for these, but then, I wasn't doing classes.

boB


More information about the Tutor mailing list