http://www.perlmonks.org?node_id=611565


in reply to Re^3: Perl / Tk Calendar - advice please.
in thread Perl / Tk Calendar - advice please.

Hmm, I guess $date stands out a little better with additional sleep and a whack on the side of the head. :-)

Perhaps my problem was that I was reading the docs and only perceived the get method for fishing data out, and didn't think about invoking get via a separate button that could access the object via a parameter. But then that's why, at midnight, I asked. :-)

That same fascination with $cd->get and my fear of MainLoop blinded me to the fact that $date was available to pass as a return value after the MainLoop finished. I was puzzling with If "get" is how you get the date, how does "get" continue to work after the MainLoop?

So, thanks to your example, the following obeys use strict and gets the value from the calendar by wrapping the appropriate code in a subroutine. Thanks!

(Interestingly in this example, the $date supplied to the ff_get sub remains frozen at 05/05/2005 since it doesn't update dynamically as global. But the value that the user clicks on the calendar, as obtained via $cd_or->get, does since the lookup method gets to fire later, i.e. when the 'Get' button is pushed.)

#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::ChooseDate; my ( $new_date ) = get_date(); print "ndt is '$new_date'\n"; sub get_date { #sets date thru the textvariable #my $date = '2006/6/6'; my $date = '4/1/2007'; #2006/6/6'; my $mw = new MainWindow; $mw->geometry('200x50'); $mw->fontCreate( 'big', -family=>'courier', -weight=>'bold', -size=>int(-18*18/14) ); my $cd = $mw->ChooseDate( -language =>'English', # changed from 'Italian' -font =>'big', # the label font -bg=>'lightsteelblue', #the label bg -textvariable=>\$date, -arrowcolor => 'black', -arrowactivecolor=>'yellow', -datecolor=> 'blue', -dateformat => 1, -orthodox => 1, # changed from '0' -daysofweekcolor=>'lightgreen', -highlightcolor=> 'pink', -linecolor=> 'green', -yearcolor=> 'black', #-command=>sub{print "$date\n"}, # quiesced this.... )->pack(-fill=>'x', -expand=>1); my $cdcan = $cd->Subwidget('canvas'); $cdcan->configure(-bg=>'black'); # bg of weekdays bar my $cdtop = $cd->Subwidget('toplevel'); $cdtop->configure(-bg=>'black'); # outline of popup # sets the date thru set $cd->set( y=>2005, m=>5, d=>5 ); my $get_button = $mw->Button( -text=>'Get', -command=>[\&ff_get, $date, $cd ], )->pack(); MainLoop; return ( $date ); } sub ff_get { my ( $date, $cd_or ) = @_; # $cd_or is an object reference print "getting....\n"; print "$date\n\n"; #print "$_[0]\n\n"; print "getting1....\n"; print $cd_or->get,"\n\n"; #print $_[1]->get,"\n\n"; }

Replies are listed 'Best First'.
Re^5: Perl / Tk Calendar - advice please.
by zentara (Archbishop) on Apr 23, 2007 at 19:48 UTC
    Yeah, that date frozen at 5/5/2005 does seem odd. I believe it is caused by the way you setup your callback on the Get button. In [] brackets, it assigns $date when first compiling, while you want it to evaluate $date at the time of the button press. It's a fine detail in Tk callbacks, and causes alot of confusion. You are not the first to get whacked :-) Try this:
    my $get_button = $mw->Button( -text=>'Get', # -command=>[\&ff_get, $date, $cd ], -command => sub{ ff_get($date,$cd) } )->pack();

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum