in reply to Tk::MsgBox and my -message string

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

Replies are listed 'Best First'.
Re^2: Tk::MsgBox and my -message string
by davidov0009 (Scribe) on Jan 06, 2008 at 03:47 UTC
    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

        I have been looking through Tk POD docs on CPAN have not read anything about a visible option or property for widgets, could you please provide a quick example of setting up visibility?

        I'm going to try setting up the dialog box in subroutine quiz_me, initialize the message with the answer and then have another sub called on the reveal answer key to configure visiblity for the message widget.


        use strict; use CGI;