[Tutor] Pmw/Tkinter question

Albert-Jan Roskam sjeik_appie at hotmail.com
Sun Mar 13 12:01:00 EDT 2016


----------------------------------------
> Date: Sat, 12 Mar 2016 23:08:20 -0600
> Subject: Re: [Tutor] Pmw/Tkinter question
> From: robertvstepp at gmail.com
> To: sjeik_appie at hotmail.com
> CC: tutor at python.org
>
> 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.

Hi Bob,

Thanks! However, if you replace 'broccoli' with a much longer option, you will see what I meant with the alignment problem. One can no longer read the first letters of the option once it's selected.
That's kinda ugly IMHO. Peter's suggestion solves it (but it uses a private/undocumented method). 

Pmw seems to be quite handy, compared to Tkinter. Not sure how it compares to tix. I suppose both offer a lot of shortcuts for many common tasks. I've only wprked with it very briefly, so I havent discovered much of it. One thing liked is the ability to easily use validators in text entry widgets, with red colors for invalid values etc.

Still, tkinter and friends will never win a beauty contest. PyQt is much more visually attractive. I was surprised motivating this could be. Qt Designer makes the process easier, but tkinter is more straightforward.

Best wishes,
Albert-Jan

 		 	   		  


More information about the Tutor mailing list