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

There are a couple of obvious ways. First the date is stored in a textvariable, so all you have to do is read $date. Second, if you read "perldoc Tk::ChooseDate", there is the get method. Example->

#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::ChooseDate; #sets date thru the textvariable my $date = '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 =>'Italian', -font =>'big', # the label font -bg=>'lightsteelblue', #the label bg -textvariable=>\$date, -arrowcolor => 'black', -arrowactivecolor=>'yellow', -datecolor=> 'blue', -dateformat => 1, -orthodox => 0, -daysofweekcolor=>'lightgreen', -highlightcolor=> 'pink', -linecolor=> 'green', -yearcolor=> 'black', -command=>sub{print "$date\n"}, )->pack(-fill=>'x', -expand=>1); my $get_button = $mw->Button(-text=>'Get', -command=>\&get)->pack(); 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 ); MainLoop; sub get{ print "getting....\n"; print "$date\n\n"; print "getting1....\n"; print $cd->get,"\n\n"; }

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

Replies are listed 'Best First'.
Re^4: Perl / Tk Calendar - advice please.
by ff (Hermit) on Apr 23, 2007 at 18:01 UTC
    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.)

      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