Two Tkinter questions (long)
Aaron Ginn
aaron.ginn at motorola.com
Wed Jul 5 18:09:24 EDT 2000
I have two questions about Tkinter. First, how do you supress a
command that is executed by a button when a widget is created? I'm
trying to create a file browser with three buttons, an OK button which
applies the selection, a filter button which filters the current
directory for a matching pattern and a cancel button which does the
obvious. The problem is that when I create the file browser in a
toplevel widget, the command that my buttons are supposed to execute
are executed immediately. For instance, here's a portion of code:
class FileBrowser:
def __init__(self, obj, dir):
self.list_of_files = []
self.list_of_dirs = []
# Set the default directory to dir.
self.filter_directory = StringVar()
self.filter_directory.set(dir)
# Set defualt filter value to all files in dir.
self.filter_pattern = StringVar()
self.filter_pattern.set('*')
self.selection_value = StringVar()
self.selection_value.set('None')
self.top = Toplevel()
< SNIP a lot of wigdets for brevity >
self.Filter = Button(self.button_frame, text = 'Filter', \
command = self.do_filter())
self.Filter.pack(side = LEFT, padx = 10)
The filter button is bound to a method called do_filter which is as
follows:
def do_filter(self):
"""
Method that generates a list of files and a list of
directories in a directory given a filter pattern,
and add them to a listbox for each.
"""
filter_path = '%s/%s' %(self.filter_directory.get(), self.filter_pattern.get())
files_matching_pattern = glob.glob(filter_path)
# Clear the listboxes
self.filelist.delete(0,END)
self.dirlist.delete(0,END)
# Add . and .. to list of directories.
self.dirlist.insert(END, '.')
self.dirlist.insert(END, '..')
# Fill the listboxes
for file in files_matching_pattern:
[head, tail] = os.path.split(file)
if os.path.isfile(file):
self.filelist.insert(END, tail)
self.list_of_files.append(tail)
elif os.path.isdir(file):
self.dirlist.insert(END, tail)
self.list_of_dirs.append(tail)
This works fine, but the command is executed as soon as the widget is
created, and then I can't execute it again. It is as if the script
has been executed and terminated but the GUI still exists. Why is the
command that button is bound to executing before I invoke the
button?
This leads to my second question. I put the command in a lambda
statement to supress it's execution. When I do this the filter
function works great, and I can execute it multiple times. However,
when I try to bind the method do_filter to an Event, the event fails
with the following error:
coronado <189> semiDRC.gui -gui
Exception in Tkinter callback
Traceback (innermost last):
File "/home/ginn/python_tk/lib/python1.5/lib-tk/Tkinter.py", line 764, in __call__
return apply(self.func, args)
File "/home/ginn/bin/semiDRC.gui", line 450, in <lambda>
self.filter_entry.bind('<Return>', lambda s=self: s.do_filter())
AttributeError: do_filter
The bind statement is
self.filter_entry.bind('<Return>', lambda s=self: s.do_filter())
Is there a problem with binding an event to a lambda function? How do
you do this? I'm very confused right now!
Aaron
--
Aaron J. Ginn Motorola SPS
Phone: (480) 814-4463 SemiCustom Solutions
Fax: (480) 814-4058 1300 N. Alma School Rd.
mailto:aaron.ginn at motorola.com Chandler, AZ 85226
More information about the Python-list
mailing list