Perl/Tk: Dialog boxes for idiots (me)
herby1620
created: 2006-05-04 18:32:48
In a previous message I asked about exiting from 'MainLoop', and got a couple of answers. One included exactly what I needed for one instance (Message Box). Now I'm trying to pop up a box (Yes, this is W-XP [yuck!]) and have a couple of selections to pick from. I thought I would use a dialog box, and I'm trying this simple code:
#!/bin/perl -w

# Try to get a simple dialog box to work.

use Tk;			# Bring in the Tk stuff.
use Tk::DialogBox;

my $mw = MainWindow->new;
# Get rid of main window, but keep some of it around.
$mw->withdraw();
print "Main window created, withdrawn\n";

my $dialog = $mw->DialogBox (-title => "Please pick one of the three",
                             -buttons => ["One", "Two", "Three"]);
my $item = $dialog->Show();				# ???
print "Returned from the show, got $item\n";

#  Done
The problem is that the above example when run on WXP hangs at the ->Show() statement. From what I can tell, the execution focus is somewhere else (it goes away from my command window) and it may be displaying something "invisable". Since it isn't displayed, I can't do anything, and it hangs. To exit, I type ^C and the interrupt kills the script. Can this be made to work? I was very able to get the messageBox to function WITHOUT going into 'MainLoop', can I do something similar here???

Your wisdom would be quite helpful. Past incantations of wisdom have been MOST helpful!! Thanks!!

Update:

It seems that the dialog box is tightly linked to the parent window. They can't (with the code above) be seperated. Even if I try to 'deiconify' the dialog box, until the main window becomes 'normal' it won't happen! Somehow 'messageBox' is able to disconnect these, but THAT is buried pretty deeply in the Tk library (I tried to look for how without luck). Still looking for "secret sauce".

Re: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-04 19:11:02
If you move the following lines of code:

# Get rid of main window, but keep some of it around.
$mw->withdraw();
print "Main window created, withdrawn\n";\


down to the last line before your "#Done" comment, the script works.
Re^2: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-04 20:36:34
Yes, putting the $mw->withdraw AFTER the $dialog->Show works. But I also get a silly empty window besides the one I want. I used 'messageBox' with the main window "withdrawn" and it works perfectly see: old post (second response). Why not this one with DialogBox. I honestly believe that it CAN be done, what is the missing "secret sauce"?
Re^3: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-04 22:11:53
No secret sauce, you are attempting to use a Show method on a sub-widget (the DialogBox) belonging to a parent widget which is withdrawn and thus invisible. If the parent is invisible the children cannot Show either.

The exception to this is the MessageBox (well, in my rotten recollection!) where you can do this:

my $mw = MainWindow->new();
$mw->withdraw();

my $ftp_warn = $mw->messageBox(
  -title   => 'Downloading upgrade',
  -message => "We are about to download an upgrade to your software, do you wish to continue?",
  -type    => 'YesNo',
  -icon    => 'question',
);
if ( $ftp_warn eq 'No' ) {
  exit;
}
else {
  &doUpdate();
}
This is a fragment fomr something that happens to be open in my editor, but it will illustrate what you need yo do. Again, "Mastering Perl/Tk" is your friend, why not go to http://safari.oreilly.com and get the 14 day free trial. That way you can learn Perl/Tk BEFORE you can get to the bookstore.

[jdtoronto]

Re^4: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-05 01:05:14
I reworked your original code in this fashion:


use strict;
use warnings;


use Tk;            
use Tk::DialogBox;
my $mw = MainWindow->new;

my $dialog = $mw->DialogBox (
	-title => "Please pick one of the three",
        -buttons => ["One", "Two", "Three"]);

my $item = $dialog->Show();                
print "Returned from the show, got $item\n";


This runs in the same fashion as your original code sans the one print statement. After scrutinizing the Tk and Tk::Dialog docs on CPAN, I believe I have the answer here.

From the docs for Tk::Dialog: "A Dialog object essentially consists of two subwidgets: a Label widget for the bitmap and a Label wigdet for the text of the dialog. If required, you can invoke the `configure' method to change any characteristic of these subwidgets."

You'll notice that the "silly box" that you refer to contains whatever you named this script in the title bar and the rest is blank. I believe that the "silly box" is in fact the Label widget for the bitmap. If so, then there is no way to not display that and display a dialog box using the Tk::Dialog module.

If someone shows that my surmise is incorrect, I'll be happy to retract this.

Update 5-05-06: Fixed a typo in the fifth line of code; I had "tmy" instead of "my".
Re^4: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-05 12:30:10
Yes, it seems as though MessageBox is the exception. As you show, one can have the main window 'withdrawn' and the message box appears. Obviously there is some flag that allows this (or am I assuming something here?). All I'm asking is that the dialog box have the same ability. I'm willing to "manually" set the proper flag (if possible) for Dialog. At the moment I've resorted to making the main window small and in one of the corners (it has nothing in it). Maybe all of this is buried deep inside Tk and it can't be changed from the Perl prespective. While I do remain hopeful, I have a solution that works for me.

Thanks to all.

Re^5: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-05 14:19:51
Well, you can iconise the MainWindow, or in Tk speak - iconify.

Although I don't have the documentation handy, the MainWindow is merely a special case of a "Toplevel" widget. Special in that it is displayed automatically when you call MainLoop. It is displayed automatically, but every other "Toplevel" must be spcifically placed using one or another of the geometry managers. Thus having created your MainWindow you should then be able to call the iconfy method:

$mw->iconify();
and have the pesky window disappear. To get it back?
$mw->deiconify();

However it may not reappear on the top of the other windows, to ensure this use the raise method:

$mw->raise();
[jdtoronto]
Re^6: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-05 16:05:55
Ah, yes, iconify, I know him well. Well, I tried that and while it nicely DOES iconify the main window, the Dialog box is iconified as well. Sorry about that. I thought it was a good idea as well.

The "find the flag" idea based on the fact that messageBox is related to Dialog is good as well. An exercise for all.

Why am I doing all of this? I'm running a script that is called by another (seperate Perl invocation) to do some tasks. I thought that having things like 'alert boxes' and 'dialog boxes' (admitally modal things) would be something desirable. So here I am. And so it goes.....

Re^5: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-05 14:37:40
Well, MessageBox is derived from Dialog, as is DialogBox. So there must be some way of doing it, but I don't think it is a simple 'find the flag' exercise. This thread has been inetersting because I have never had a situation where I wanted a DialogBox without the MainWindow.

jdtoronto

Re^5: Perl/Tk: Dialog boxes for idiots (me)
created: 2006-05-05 16:23:36
Use the source, Luke! ( Look at the source to MessageBox() in Tk.pm ). It seems to just use Tk::Dialog. It shouldn't be too hard to figure out what the secret is to getting the results you want.

perlmonks.org content © perlmonks.org and herby1620, jdtoronto, runrig, Scott7477

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03