Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl/Tk - open window by event 1

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
I guess it's a simple one.

I have a text displayed in the main window.
When some even occurs in the flow of the main sub - I want to open another window to ask a user for an input.

Optionally - I'd like to get a user input ("are you sure?") from my text widget,but I do not think it's possible.

Aprreciate any help.
Thanks

Long live king Moshiach !
 
These things are both possible.

There are two things you can use for Dialog boxes.

Tk::Dialog is for simple quick-and-easy dialogs, where you just have a text message you want to send:

Code:
use Tk::Dialog;

my $dialog = $mw->Dialog (
   -title   => 'Prompt',
   -text    => 'Are you sure?',
   -buttons => [ 'Yes', 'No' ],
   -default_button => 'Yes',
);

my $choice = $dialog->Show;
if ($choice eq 'Yes') {
}
elsif ($choice eq 'No') {
}

Tk::DialogBox is similar to Tk::Dialog, but you can put any widgets you want into it.

Code:
use Tk::DialogBox;

my $dialog = $mw->DialogBox (
   -title => 'Prompt',
   -buttons => [ 'OK', 'Cancel' ],
   -default_button => 'OK',
);

# add a couple widgets
$dialog->add ('Label',
   -text => 'Enter Your Password',
   -font => [
      -weight => 'bold',
   ],
   -foreground => 'red',
);
$dialog->add ('Entry',
   -textvariable => \$password,
);

my $choice = $dialog->Show;
if ($choice eq 'OK') {
}
else {
}

Tk::Dialog and Tk::DialogBox both will block their parent window while they are present. I.E. when a dialog box appears, clicking things on the window that spawned it ($mw) won't do anything.

If you're on a Windows machine, there's a quick and easy alternative for simple prompts: using the Win32 message boxes.

Code:
my $choice = $mw->messageBox (
   -title   => 'Prompt',
   -type    => 'YesNoCancel',
   -icon    => 'info',
   -message => 'Are you sure?',
);

While we're on the topic of new windows, I should also mention Toplevel.

Toplevel windows act just like MainWindow windows. They don't block their parent window. It's just like having multiple MainWindows.

The only thing with Toplevels, though, is that they'll close when their parent window closes.

Code:
my $mw = MainWindow->new (
   -title => 'Main Window',
);

$mw->Label (-text => 'Main Window')->pack;

my $child = $mw->Toplevel (
   -title => 'Toplevel',
);

$child->Label (-text => 'Toplevel')->pack;
 
Search Google for most of the Tk things you need (I tend to start my searches with "Perl/Tk" and then what I'm looking for, i.e. "Perl/Tk menu" or "Perl/Tk icons" etc.)

Otherwise, check the Tk:: modules on the CPAN page, every Tk:: module uploaded to CPAN is on there, so the more you browse, the more cool widgets you'll find. :)
 
OK.

I've gone here fr a Tk:DialogBox :

my $dialog = $mw->DialogBox (-title => 'Prompt',-buttons => [ 'Ok', 'Cancel' ], #define the final dialog
-default_button => 'OK',);

$dialog->add ('Label', -text => 'Ok to delete all saved data ?', -font => [ -weight => 'bold'],
-foreground => 'red',);
MainLoop;
#################################
Then later on in code:
. . .
my $REPLY = $dialog->Show;


But the "Label" with the text does not show at all.
What could be wrong ?


Long live king Moshiach !
 
Sorry, I haven't used Tk::DialogBox in quite a while, so I looked back at some of my old code.

I think you need to pack the add commands too...

Code:
		my $about = $main->DialogBox (-title => $lang->{help_about}, -buttons => ['Close']);
		$about->geometry ('300x110');
		$about->Icon (-image => $icon);

		my $about_a = $about->add ('Label',
			-text => $lang->{help_title},
			-font => [
				-family => 'Verdana',
				-size   => 14,
				-weight => 'bold',
			],
		)->pack;
		my $about_b = $about->add ('Label',
			-text => $lang->{help_desc},
			-font => [
				-family => 'Verdana',
				-size => 10,
			],
		)->pack;

		$about->Show;

In another place in the same code I had this:

Code:
			my $about = $main->DialogBox (-title => $lang->{ext_about}, -buttons => ['OK']);
			$about->geometry ('300x200');
			$about->Icon (-image => $icon);

			my $about_a = $about->Scrolled ('ROText',
				-scrollbars => 'e',
				-wrap       => 'word',
				-foreground => '#000000',
				-background => '#FFFFFF',
				-font       => [
					-family => 'Verdana',
					-size   => 8,
				],
			)->pack (-fill => 'both', -expand => 1);

			# Insert the data.
			$about_a->delete ('1.0','end');
			$about_a->insert ('end',"About Extensions\n\n"
				. "Extensions are Perl files with the extension of .CP and are included "
				. "by CPad after the menubar is created, giving extensions full control "
				. "to insert menu items.\n\n"
				. "The standard location for extensions to be put into the menubar is in "
				. "the \"Extensions\" menu, however extensions can do whatever they want.");

			$about->Show;

And that also appeared to work, so I guess you can pack normal widgets into a DialogBox like you would with a MainWindow. I guess it just prefers the add() method so you won't accidentally interfere with the button frame.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top