📄 ch15_16.htm
字号:
<HTML><HEAD><TITLE>Recipe 15.15. Creating Dialog Boxes with Tk (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:43:30Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"><LINKREL="prev"HREF="ch15_15.htm"TITLE="15.14. Creating Menus with Tk"><LINKREL="next"HREF="ch15_17.htm"TITLE="15.16. Responding to Tk Resize Events"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_15.htm"TITLE="15.14. Creating Menus with Tk"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.14. Creating Menus with Tk"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_17.htm"TITLE="15.16. Responding to Tk Resize Events"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.16. Responding to Tk Resize Events"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch15-22653">15.15. Creating Dialog Boxes with Tk</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1591">Problem<ACLASS="indexterm"NAME="ch15-idx-1000005184-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005184-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005184-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005184-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005184-4"></A></A></H3><PCLASS="para">You want to create a dialog box, i.e., a new top-level window with buttons to make the window go away. The dialog box might also have other items, such as labels and text entry widgets for creating a fill-out form. You could use such a dialog box to collect registration information, and you want it to go away when registration is sent or if the user chooses not to register.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1597">Solution</A></H3><PCLASS="para">For simple jobs, use the Tk::DialogBox widget:</P><PRECLASS="programlisting">use Tk::DialogBox;$dialog = $main->DialogBox( -title => "Register This Program", -buttons => [ "Register", "Cancel" ] );# add widgets to the dialog box with $dialog->Add()# later, when you need to display the dialog box$button = $dialog->Show();if ($button eq "Register") { # ...} elsif ($button eq "Cancel") { # ...} else { # this shouldn't happen}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1635">Discussion</A></H3><PCLASS="para">A DialogBox has two parts: the bottom is a set of buttons, and the top has the widgets of your choosing. <CODECLASS="literal">Show</CODE>ing a DialogBox pops it up and returns the button the user selected.</P><PCLASS="para"><ACLASS="xref"HREF="ch15_16.htm#ch15-32379"TITLE="tksample3">Example 15.6</A> contains a complete program demonstrating the DialogBox.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch15-32379">Example 15.6: tksample3</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch15-idx-1000005008-0"></A>tksample3 - demonstrate dialog boxesuse Tk;use Tk::DialogBox;$main = MainWindow->new();$dialog = $main->DialogBox( -title => "Register", -buttons => [ "Register", "Cancel" ], );# the top part of the dialog box will let people enter their names,# with a Label as a prompt$dialog->add("Label", -text => "Name")->pack();$entry = $dialog->add("Entry", -width => 35)->pack();# we bring up the dialog box with a button$main->Button( -text => "Click Here For Registration Form", -command => \&register) ->pack(-side => "left");$main->Button( -text => "Quit", -command => sub { exit } ) ->pack(-side => "left");MainLoop;## register## Called to pop up the registration dialog box#sub register { my $button; my $done = 0; do { # show the dialog $button = $dialog->Show; # act based on what button they pushed if ($button eq "Register") { my $name = $entry->get; if (defined($name) && length($name)) { print "Welcome to the fold, $name\n"; $done = 1; } else { print "You didn't give me your name!\n"; } } else { print "Sorry you decided not to register.\n"; $done = 1; } } until $done;}</PRE></DIV><PCLASS="para">The top part of this DialogBox has two widgets: a label and a text entry. To collect more information from the user, we'd have more labels and text entries.</P><PCLASS="para">A common use of dialog boxes is to display error messages or warnings. The program in <ACLASS="xref"HREF="ch15_16.htm#ch15-18118"TITLE="tksample4">Example 15.7</A> demonstrates how to display Perl's <CODECLASS="literal">warn</CODE><ACLASS="indexterm"NAME="ch15-idx-1000005188-0"></A> function in a DialogBox.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch15-18118">Example 15.7: tksample4</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch15-idx-1000005010-0"></A>tksample4 - popup dialog boxes for warningsuse Tk;use Tk::DialogBox;my $main;# set up a warning handler that displays the warning in a Tk dialog boxBEGIN { $SIG{__WARN__} = sub { if (defined $main) { my $dialog = $main->DialogBox( -title => "Warning", -buttons => [ "Acknowledge" ]); $dialog->add("Label", -text => $_[0])->pack; $dialog->Show; } else { print STDOUT join("\n", @_), "n"; } };}# your program goes here$main = MainWindow->new();$main->Button( -text => "Make A Warning", -command => \&make_warning) ->pack(-side => "left");$main->Button( -text => "Quit", -command => sub { exit } ) ->pack(-side => "left");MainLoop;# dummy subroutine to generate a warning sub make_warning { my $a; my $b = 2 * $a;}<ACLASS="indexterm"NAME="ch15-idx-1000005190-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005190-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005190-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005190-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005190-4"></A></PRE></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1847">See Also</A></H3><PCLASS="para">The Tk::DialogBox manpage in the documentation for the Tk module from CPAN; the <ICLASS="filename">menu</I> (n) manpage (if you have it)</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_15.htm"TITLE="15.14. Creating Menus with Tk"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.14. Creating Menus with Tk"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_17.htm"TITLE="15.16. Responding to Tk Resize Events"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.16. Responding to Tk Resize Events"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">15.14. Creating Menus with Tk</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">15.16. Responding to Tk Resize Events</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -