Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Tk::MsgBox and my -message string

by davidov0009 (Scribe)
on Jan 05, 2008 at 23:36 UTC ( [id://660592]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,

I have just delved into using Tk to design a simple flash card study program to help me review for mid-term exams. It uses a hash tied to a dbm file. I want to display the back of the card (i.e. the answer) in a popup message dialog when the "Flip" button is pressed. As so far I can get the message box to appear, but not the answer that should go inside of it (stored in $value).

Any help appreciated. Here is the code:
#!/usr/bin/perl $VERSION = 1.00; use strict; use Tk; use Tk::FileSelect; use Tk::MsgBox; my ($value, %deck); #main window my $mw = MainWindow->new(); $mw->geometry("400x200"); #file selector my $file_dialog = $mw->Button( -text => 'Load Cards', -command => \&load_deck ) ->grid(-row=>6,-column=>0); #flip button my $flip = $mw->Button( -text => 'Flip', -command => \&flip_card ) ->grid(-row=>6,-column=>2); #next button my $next = $mw->Button( -text => 'Next =>', -command => \&next_card ) ->grid(-row=>6,-column=>4); #front label & display $mw->Label( -text => 'Front: ' )->grid(-row=>0); my $front = $mw->Label()->grid(-row=>0,-column=>2); #back display my $back = $mw->MsgBox(-title=>'Back of card',-default=>'ok',-message= +>$value); $mw->Button( -text => 'Quit', -command => sub { exit } ) ->grid(-row=>7,-column=>2); MainLoop; sub load_deck { my $start_dir = "/home/$ENV{'USER'}/TkPerl"; my $FSref = $mw->FileSelect(-directory => $start_dir); my $file = $FSref->Show; dbmopen(%deck, $file, 0644); next_card(); } sub next_card { my ($key, $loc_val) = each (%deck); $value = $loc_val; $front->configure(-text=>$key); } sub flip_card { my $pop = $back->Show; }

use strict; use CGI;

Replies are listed 'Best First'.
Re: Tk::MsgBox and my -message string
by NetWallah (Canon) on Jan 06, 2008 at 00:11 UTC
    Update:Text revised - essentially the same idea, but propose a more reasonable implementation:

    The population of the $back variable needs to be updated INSIDE sub "next_card".

    In particular,

    #back display -message=>$value
    for the $back variable must be set AFTER a new $value is obtained, inside "next_card".

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      Thanks that's worked out well, I've been changing around my implementation a bit. One last question. I setup the msgboxes in the MainLoop and I intialize their message values in a subroutine. When I run the program I get two blank msgboxes at startup (supposedly from the setup statements for them in the MainLoop). How can I avoid this?
      Code below:
      #!/usr/bin/perl $VERSION = 1.00; use strict; use Tk; use Tk::FileSelect; use Tk::MsgBox; my (%deck); #main window my $mw = MainWindow->new(); $mw->geometry("400x200"); #file selector button my $file_dialog = $mw->Button( -text => 'Load Cards', -command => \&load_deck ) ->grid(-row=>6,-column=>0); #start quiz button $mw->Button( -text => 'Quiz Me!', -command => \&quiz_me ) ->grid(-row=>6,-column=>2); #quit button $mw->Button( -text => 'Quit', -command => sub { exit } ) ->grid(-row=>6,-column=>4); #front & back popups # Here is where I believe those 2 blank ones at startup are coming fro +m... my $front = $mw->MsgBox(-title=>'FRONT',-default=>'ok',-icon=>'questio +n'); my $back = $mw->MsgBox(-title=>'BACK',-default=>'ok',-icon=>'info'); MainLoop; sub load_deck { my $start_dir = "/home/$ENV{'USER'}/TkPerl"; my $FSref = $mw->FileSelect(-directory => $start_dir); my $file = $FSref->Show; dbmopen(%deck, $file, 0644); } sub quiz_me { while ( my ($key, $value) = each (%deck) ) { #Configure the message value and then show the msgboxes $front->configure(-message=>$key); $front->Show; $back->configure(-message=>$value); $back->Show; } }

      use strict; use CGI;
        The Tk::MsgBox is intended to be a simplified widget that displays as soon as it is created.

        So, your suspicion is correct. To fix it, move the creation of the MsgBox to the point where the value is available (inside "quiz_me"). You do not reference $front and $back elsewhere in any case.

        You might want to consider making your interface a little nicer by using a Tk::DialogBox , when you get tired of annoying popup dialogs. The dialogBox could be pre-populated with the answer, which will have its VISIBLE property off. A button can then be pressed to REVEAL it. It would also help to have a NEXT button to step through the quiz. Essentially, your app becomes a single dialog box, which is a pretty standard way of implementing simple stuff.

             "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: Tk::MsgBox and my -message string
by eserte (Deacon) on Jan 06, 2008 at 10:42 UTC
    Uh, a bug in Tk::MsgBox. You can workaround it by adding $back->withdraw after creation of the msgbox. Or wait for the next Perl/Tk release (probably some time in spring 2008).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found