By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

Label position on Homebrew Pmw Megawidget

P: n/a
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... |
---------------------- ------------

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\file | |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.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=1, column=1)

# 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=1, column=2)

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

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

# Create and pack a FileSelect widgets
widgets = []

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)
root.mainloop()

------------- cut here -------------------------------
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.