Label position on Homebrew Pmw Megawidget

Peter Otten __peter__ at web.de
Sat Feb 21 10:19:49 CET 2004


Greg wrote:

> I'm trying to create a megawidget for file selection that contains a
> Label, EntryField, and Browse Button (that will open a tkFileDialog).
> The label positioning won't cooperate however, could anyone help with
> this?
> 
> Here's what I want:
> labelpos='w':
>            ---------------------- ------------
> Filename:  |c:\directory\file   | |Browse... |
>            ---------------------- ------------

[...]

> My code is below.  I've tried different grid(row,column) values for
> the widgets, but that didn't seem to help.
> 
> 
> Can anyone help with this?  It's driving me nuts! I imagine I'm doing
> something wrong with the createlabel() method, or something else
> trivially simple...

I've no clue either. Enter brute force:

#!/bin/python
#Running standalone will demo the above ascii pic, plus some others:

import Pmw
import Tkinter
from Tkinter import *
import tkFileDialog

class FileSelect(Pmw.MegaWidget):
  """ Megawidget containing Pmw.Entryfield and a Browse button
      for file selection
  """

  def __init__ (self, parent=None, **kw):
    # Define the megawidget options
    optiondefs = (
                   ('labelmargin', 0,    Pmw.INITOPT),
                   ('labelpos',    None, Pmw.INITOPT),
                 )
    self.defineoptions(kw, optiondefs)

    # Initialise base class (after defining options)
    Pmw.MegaWidget.__init__(self, parent)

    # Create the components
    interior = self.interior()

    # Create a label
    self.createlabel(interior, childRows=2, childCols=1)

    # Create the Entryfield component
    self.entryfield = self.createcomponent('filename',      # Name
                                           (),              # Aliases
                                           None,            # Group
                                           Pmw.EntryField,  # Class
                                           (interior,),     # Constructor
Args
                                          )
    self.entryfield.grid(row=yoff, column=xoff)

    # Create the Browse Button
    self.browsebutton = self.createcomponent('browsebutton', # Name
                                             (),             # Aliases
                                             None,           # Group
                                             Tkinter.Button, # Class
                                             interior,       # Constructor
Args
                                             text="Browse...",
                                             command=self.getFileName
                                            )
    self.browsebutton.grid(row=yoff, column=xoff+1)

    # Check keywords and initialise options
    self.initialiseoptions()

  #------------------------------------------------------------------
  # Popup a file select dialog and fill in the entry field with the
  # chose filename
  #------------------------------------------------------------------

  def getFileName (self):
    dialog = tkFileDialog.Open()
    fname = dialog.show()
    if fname != "":
      self.entryfield.setvalue(fname)

xoff = 0
yoff = 0

def terminate():
    import sys
    sys.exit(0)

def testWidgets():
  root = Tkinter.Tk()

  # Create and pack a FileSelect widgets
  widgets = []
  Tkinter.Button(root, text="terminate app", command=terminate).pack()
  Tkinter.Button(root, text="next try", command=root.quit).pack()
  widgets.append(FileSelect(labelpos='n', label_text="north"))
  widgets.append(FileSelect(labelpos='e', label_text="east"))
  widgets.append(FileSelect(labelpos='s', label_text="south"))
  widgets.append(FileSelect(labelpos='w', label_text="west"))
  widgets.append(FileSelect(labelpos='ws', label_text="westsouth"))
  widgets.append(FileSelect(labelpos='wn', label_text="westnorth"))
  widgets.append(FileSelect(labelpos='sw', label_text="southwest"))
  widgets.append(FileSelect(labelpos='nw', label_text="northwest"))

  map(lambda w: w.pack(pady=20), widgets)
  return root

if __name__ == "__main__":
    for xoff in range(3):
        for yoff in range(3):
            root = testWidgets()
            root.title("Testing xoff=%d, yoff=%d" % (xoff, yoff))
            root.mainloop()
            root.destroy()

(xoff=1, yoff=2) seems promising.

Peter



More information about the Python-list mailing list