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