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