Tkinter Callback question

Steve Holden sholden at holdenweb.com
Mon Aug 12 16:38:51 EDT 2002


"Jack B." <barfsky at hotmail.com> wrote in message
news:6f37c65.0208121204.1bf05117 at posting.google.com...
> Perhaps someone can help me with the proper syntax using the
> "command=" option while making a menu button.  Here's what I want to
> do:
>
> for number in range(-4,1):
>   temp = get_dates.dbdate(number)
>   date_menu.add_command(label=temp, command=self.set_valid_date(temp)
> menubar.add_cascade(label="Choose Date", menu=date_menu)
>
> (get_dates.dbdate will return a correctly formatted date, given number
> of days back or forward.  set_valid_date will set act on the date
> passed)
>
> This code will list dates from today back 4 days but I can't seem to
> call the "command" part and pass any data.  The command executes
> immediatly, and the buttons created do nothing.  Surely there is a way
> to do this?  The only way I can execute a command is by NOT passing
> any data, ie.. command=self.hello.
>

You have actually recognized your own error. The "command" argument should
be a REFERENCE to a parameterless function. Your original syntax was indeed
passing the result of CALLING the function, with an argument.

When you really want to pass an argument into the function, to take
advantage of commonality, things get a bit more complicated. You might want
to look at binding events to a button subclass of your own devising: using
command callbacks is the easiest way to trigger event-driven code, but
providing your own event bindings allows you to get at the attributes of the
event, making it practical (for example) to locate the button that was
clicked on and extract its label for use as the argument to your
set_valid_date() method..

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list