Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Callbacks using TK::Minicalendar

by reaper9187 (Scribe)
on Aug 23, 2014 at 14:22 UTC ( [id://1098412]=perlquestion: print w/replies, xml ) Need Help??

reaper9187 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I need your help regarding the use of Tk::Minicalendar in one of my application. I have several instances where the dates are required to enter dates for some application. Now, I am trying to create a separate subroutine to retrieve the dates. Of all the widgets that I have come across so far, Tk::Minicalendar has caught my attention for its more appaling interface.

However, when I try to create a separate sub for this widget, I encounter a problem. I am not able to return selected dates through the function callback. For some reason, the selected date is not returned. I know I am going wrong somewhere, just don't know where. Here is a sample code
use Tk; use Tk::MiniCalendar; use strict; my $date_sel = dates(); print $date_sel; MainLoop; sub dates{ my $out_date; my $top = MainWindow->new; my $frm1 = $top->Frame->pack; # Frame to place MiniCalendar in my $minical = $frm1->MiniCalendar->pack; my $frm2 = $top->Frame->pack; # Frame for Ok Button my $b_ok = $frm2->Button(-text => "Ok", -command => sub { my ($year, $month, $day) = $minical->date; $out_date = "$day-$month-$year"; exit; }, )->pack; return $out_date; }
Thanks in advance for your help .. :)

Replies are listed 'Best First'.
Re: Callbacks using TK::Minicalendar
by zentara (Archbishop) on Aug 23, 2014 at 15:04 UTC
    I think you have 2 problems. 1 is that you have the MainLoop statement in the wrong place, and 2, you call exit, instead of destroying the MainLoop.

    This works:

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::MiniCalendar; my $date_sel = dates(); print $date_sel; # MainLoop; # bad placement sub dates{ my $out_date; my $top = MainWindow->new; my $frm1 = $top->Frame->pack; # Frame to place MiniCalendar in my $minical = $frm1->MiniCalendar->pack; my $frm2 = $top->Frame->pack; # Frame for Ok Button my $b_ok = $frm2->Button(-text => "Ok", -command => sub { my ($year, $month, $day) = $minical->date; $out_date = "$day-$month-$year"; # exit; $top->destroy; }, )->pack; MainLoop; return $out_date; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thank you very much zentara for your prompt reply. I have two more questions though:

      1. Is there an option to anchor the widget window to the button (i.e from where this callback was initiated) instead of freely floating in space ??

      2. The more dumber question :
      I have several such widgets as a part of my application. Will specifying the Mainloop in this subroutine cause any unwanted bugs later ?? I have to implement the MainLoop in my main code after packing the widgets.
      If I define the MainLoop outside the scope of this subroutine, the code doesn't work. So where should the ideal placement of Mainloop be ?
        Hi, for question 1, there is the way to get coordinates of any widget or the mouse cursor for that matter. This is from Mastering Perl/Tk Chapter 13.10
        # You can get the coordinates of the upper-left corner of a widget by # using the x and y methods. # The coordinates they return are relative to the parent of the widget +: $x = $widget->x; $y = $widget->y; # for a mouse pointer my ($x, $y) = $mw->pointerxy;
        Then all you need to do is specify a -geometry statement for your toplevel window, to place it at that position.

        For question #2, you need to understand that a Mainwindow or your $top, is initmately connected to the MainLoop statement. When you destroy one, you destroy the eventloop. So you would need to keep your multiple MainLoops, each in their own subroutine or closure, as there can only be 1 MainLoop running at any time( with certain odd exceptions). So it makes me wonder if your basic program design is correct, maybe you could explain better what you are trying to do, and why you need multiple eventloops.

        If you have the need for making multiple toplevel windows within your program, you should consider letting them all share the same MainLoop, and instead of $top->destroy, you could use $top->packForget to just remove the calendar from view, allowing the same MainLoop to keep running for your next use. A MainLoop can have multiple toplevel windows called and or destroyed, as long as the $mw associated with the MainLoop is still intact.

        It would look something like this: You make 1 mainwindow which controls the MainLoop, then you can add as many toplevel windows as often as you like.

        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->title( "MainWindow" ); $mw->geometry('400x400+30+30'); $mw->Button( -text => "Toplevel", -command => \&do_Toplevel )->pack(); $mw->waitVisibility; my $tl; &do_Toplevel; #initialize it in withdrawn state MainLoop; sub do_Toplevel { my $topx = 300; my $topy = 100; # print $mw->geometry,"\n"; my ($xs,$ys,$signx,$xp,$signy,$yp) = $mw->geometry =~ /(\d+)x(\d+)([+-]+)(\d+)([+-]+)(\d+)/; # print "$xs,$ys,$signx,$xp,$signy,$yp\n"; my $xpos = int($xp + $xs/2 - $topx/2); my $ypos = int($yp + $ys/2 - $topy/2); if ( !Exists( $tl ) ) { $tl = $mw->Toplevel(); $tl->geometry($topx.'x'.$topy.'+'.$xpos.'+'.$ypos); $tl->title( "Toplevel" ); #put all your widgets here $tl->Button( -text => "Close", -command => sub { $tl->grabRelease; $tl->withdraw } )->pack; $tl->withdraw; } else { $tl->geometry($topx.'x'.$topy.'+'.$xpos.'+'.$ypos); $tl->deiconify(); $tl->raise(); $tl->grabGlobal; } } ##############################################################

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: Callbacks using TK::Minicalendar
by reaper9187 (Scribe) on Aug 24, 2014 at 10:37 UTC
    Hi Zentara,
    Thank you very much for taking the time to reply to my posts. I really appreaciate your help in this regard.

    My application is pretty big to be able to discuss here. However, just to give you a gist of the code, I'll discuss some salient features of the application here.

    I have a bunch of features/functions that I need to carry out using my application. I've designed the framework of the application to mimic a commercial application similar to what is given here.

    As such, I am reusing the space on the right to the maximum extent possible by reusing the canvas for the perl Tk widgets using the packForget to avoid the mess of TopLevel widgets for every feature of the program, while maintaing ease of access for the user thereby keeping it simple. However, I have reserved the use of Top level windows for trivial functions like retrieving the dates,etc.

    I have not faced any issues so far with packForget options. It is only for a couple of subroutines where I implement m top level widgets where I feel my code is broken. I will look into your suggestions and implement them. Thank you very much ..!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1098412]
Approved by stefbv
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found