Label position on Homebrew Pmw Megawidget

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg

    Label position on Homebrew Pmw Megawidget

    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\f ile | |Browse... |
    ---------------------- ------------

    No matter what I set as labelpos, the label will not end up on the
    same row as the other widgets. Some others are also not working as
    expected:

    labelpos='w':
    ---------------------- ------------
    |c:\directory\f ile | |Browse... |
    ---------------------- ------------
    Filename:

    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...

    ------------- cut here -------------------------------
    #!/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.defineopti ons(kw, optiondefs)

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

    # Create the components
    interior = self.interior()

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

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

    # Create the Browse Button
    self.browsebutt on = self.createcomp onent('browsebu tton', # Name
    (), # Aliases
    None, # Group
    Tkinter.Button, # Class
    interior, #
    Constructor Args
    text="Browse... ",
    command=self.ge tFileName
    )
    self.browsebutt on.grid(row=1, column=2)

    # Check keywords and initialise options
    self.initialise options()

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

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

    # Standalone demo
    if __name__ == "__main__":
    root = Tkinter.Tk()

    # Create and pack a FileSelect widgets
    widgets = []

    widgets.append( FileSelect(labe lpos='n', label_text="nor th"))
    widgets.append( FileSelect(labe lpos='e', label_text="eas t"))
    widgets.append( FileSelect(labe lpos='s', label_text="sou th"))
    widgets.append( FileSelect(labe lpos='w', label_text="wes t"))
    widgets.append( FileSelect(labe lpos='ws', label_text="wes tsouth"))
    widgets.append( FileSelect(labe lpos='wn', label_text="wes tnorth"))
    widgets.append( FileSelect(labe lpos='sw', label_text="sou thwest"))
    widgets.append( FileSelect(labe lpos='nw', label_text="nor thwest"))

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

    ------------- cut here -------------------------------
  • Peter Otten

    #2
    Re: Label position on Homebrew Pmw Megawidget

    Greg wrote:
    [color=blue]
    > 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\f ile | |Browse... |
    > ---------------------- ------------[/color]

    [...]
    [color=blue]
    > 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...[/color]

    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.defineopti ons(kw, optiondefs)

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

    # Create the components
    interior = self.interior()

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

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

    # Create the Browse Button
    self.browsebutt on = self.createcomp onent('browsebu tton', # Name
    (), # Aliases
    None, # Group
    Tkinter.Button, # Class
    interior, # Constructor
    Args
    text="Browse... ",
    command=self.ge tFileName
    )
    self.browsebutt on.grid(row=yof f, column=xoff+1)

    # Check keywords and initialise options
    self.initialise options()

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

    def getFileName (self):
    dialog = tkFileDialog.Op en()
    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=termina te).pack()
    Tkinter.Button( root, text="next try", command=root.qu it).pack()
    widgets.append( FileSelect(labe lpos='n', label_text="nor th"))
    widgets.append( FileSelect(labe lpos='e', label_text="eas t"))
    widgets.append( FileSelect(labe lpos='s', label_text="sou th"))
    widgets.append( FileSelect(labe lpos='w', label_text="wes t"))
    widgets.append( FileSelect(labe lpos='ws', label_text="wes tsouth"))
    widgets.append( FileSelect(labe lpos='wn', label_text="wes tnorth"))
    widgets.append( FileSelect(labe lpos='sw', label_text="sou thwest"))
    widgets.append( FileSelect(labe lpos='nw', label_text="nor thwest"))

    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("Tes ting xoff=%d, yoff=%d" % (xoff, yoff))
    root.mainloop()
    root.destroy()

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

    Peter

    Comment

    Working...