📄 ch15_15.htm
字号:
<HTML><HEAD><TITLE>Recipe 15.14. Creating Menus 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:28Z"><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_14.htm"TITLE="15.13. Controlling Another Program with Expect"><LINKREL="next"HREF="ch15_16.htm"TITLE="15.15. Creating Dialog Boxes with Tk"></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_14.htm"TITLE="15.13. Controlling Another Program with Expect"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.13. Controlling Another Program with Expect"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_16.htm"TITLE="15.15. Creating Dialog Boxes with Tk"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.15. Creating Dialog Boxes with Tk"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch15-27780">15.14. Creating Menus with Tk</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1391">Problem<ACLASS="indexterm"NAME="ch15-idx-1000005154-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005154-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005154-2"></A></A></H3><PCLASS="para">You want to create a <ACLASS="indexterm"NAME="ch15-idx-1000005170-0"></A>window that has a menu bar at the top.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1397">Solution</A></H3><PCLASS="para">Use the Tk <ACLASS="indexterm"NAME="ch15-idx-1000005163-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005163-1"></A>Menubutton and Frame widgets:</P><PRECLASS="programlisting">use Tk;$main = MainWindow->new();# Create a horizontal space at the top of the window for the# menu to live in.$menubar = $main->Frame(-relief => "raised", -borderwidth => 2) ->pack (-anchor => "nw", -fill => "x");# Create a button labeled "File" that brings up a menu$file_menu = $menubar->Menubutton(-text => "File", -underline => 1) ->pack (-side => "left" );# Create entries in the "File" menu$file_menu->command(-label => "Print", -command => \&Print);</PRE><PCLASS="para">This is considerably easier if you use the <CODECLASS="literal">-menuitems</CODE> shortcut:</P><PRECLASS="programlisting">$file_menu = $menubar->Menubutton(-text => "File", -underline => 1, -menuitems => [ [ Button => "Print",-command => \&Print ], [ Button => "Save",-command => \&Save ] ]) ->pack(-side => "left");</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1453">Discussion</A></H3><PCLASS="para">Menus in applications can be viewed as four separate components working together: Frames, Menubuttons, Menus, and Menu Entries. The Frame is the horizontal bar at the top of the window that the menu resides in (the <EMCLASS="emphasis">menubar</EM>). Inside the Frame are a set of Menubuttons, corresponding to Menus: File, Edit, Format, Buffers, and so on. When the user clicks on a Menubutton, the Menubutton brings up the corresponding Menu, a vertically arranged list of Menu Entries.</P><PCLASS="para">Options on a Menu are <EMCLASS="emphasis">labels</EM><ACLASS="indexterm"NAME="ch15-idx-1000005171-0"></A> (Open, for example) or <EMCLASS="emphasis">separators</EM><ACLASS="indexterm"NAME="ch15-idx-1000005172-0"></A> (horizontal lines dividing one set of entries from another in a single menu).</P><PCLASS="para">The <EMCLASS="emphasis">command</EM><ACLASS="indexterm"NAME="ch15-idx-1000005173-0"></A> entry, like Print in the File menu above, has code associated with it. When the entry is selected, the command is run by invoking the callback. These are the most common:</P><PRECLASS="programlisting"> $file_menu->command(-label => "Quit Immediately", -command => sub { exit } );</PRE><PCLASS="para">Separators don't have any action associated with them:</P><PRECLASS="programlisting">$file_menu->separator();</PRE><PCLASS="para">A <EMCLASS="emphasis">checkbutton</EM><ACLASS="indexterm"NAME="ch15-idx-1000005174-0"></A> menu entry has an on value, an off value, and a variable associated with it. If the variable has the on value, the checkbutton menu entry has a check beside its label. If the variable has the off value, it does not. Selecting the entry on the menu toggles the state of the variable.</P><PRECLASS="programlisting">$options_menu->checkbutton(-label => "Create Debugging File", -variable => \$debug, -onvalue => 1, -offvalue => 0);</PRE><PCLASS="para">A group of <EMCLASS="emphasis">radiobuttons</EM><ACLASS="indexterm"NAME="ch15-idx-1000005175-0"></A> is associated with a single variable. Only one radiobutton associated with that variable can be on at any time. Selecting a radiobutton gives the variable the value associated with it:</P><PRECLASS="programlisting">$debug_menu->radiobutton(-label => "Level 1", -variable => \$log_level, -value => 1);$debug_menu->radiobutton(-label => "Level 2", -variable => \$log_level, -value => 2);$debug_menu->radiobutton(-label => "Level 3", -variable => \$log_level, -value => 3);</PRE><PCLASS="para">Create nested menus with the <EMCLASS="emphasis">cascade</EM><ACLASS="indexterm"NAME="ch15-idx-1000005176-0"></A> menu entry. For instance, under <EMCLASS="emphasis">Netscape Navigator</EM>, the File menu button at the left has a cascade menu entry New that brings up a selection of new windows. Creating a cascading menu entry is trickier than creating the other menu entries. You must create a cascade menu entry, fetch the new menu associated with that menu entry, and create entries on that new menu.</P><PRECLASS="programlisting"># step 1: create the cascading menu entry$format_menu->cascade (-label => "Font");# step 2: get the new Menu we just made$font_menu = $format_menu->cget("-menu");# step 3: populate that Menu$font_menu->radiobutton (-label => "Courier", -variable => \$font_name, -value => "courier");$font_menu->radiobutton (-label => "Times Roman", -variable => \$font_name, -value => "times");</PRE><PCLASS="para">A <EMCLASS="emphasis">tear-off</EM><ACLASS="indexterm"NAME="ch15-idx-1000005164-0"></A> menu entry lets the user move the menu that it is on. By default, all Menubuttons and cascade menu entries make Menus that have a tear-off entry at the top of them. To create Menus without that default, use the <CODECLASS="literal">-tearoff</CODE> option:</P><PRECLASS="programlisting">$format_menu = $menubar->Menubutton(-text => "Format", -underline => 1 -tearoff => 0) ->pack;$font_menu = $format_menu->cascade(-label => "Font", -tearoff => 0);</PRE><PCLASS="para">The <CODECLASS="literal">-menuitems</CODE> option to <CODECLASS="literal">Menubutton</CODE> is a shorthand for creating these menubuttons. Pass it an array reference representing the options on the Menubutton. Each option is itself an anonymous array. The first two elements of the option array are the button type (<CODECLASS="literal">"command"</CODE>, <CODECLASS="literal">"radiobutton"</CODE>, <CODECLASS="literal">"checkbutton"</CODE>, <CODECLASS="literal">"cascade"</CODE>, or <CODECLASS="literal">"tearoff"</CODE>) and the menu name.</P><PCLASS="para">Here's how to use <CODECLASS="literal">menuitems</CODE> to make an Edit menu:</P><PRECLASS="programlisting">my $f = $menubar->Menubutton(-text => "Edit", -underline => 0, -menuitems => [ [Button => 'Copy', -command => \&edit_copy ], [Button => 'Cut', -command => \&edit_cut ], [Button => 'Paste', -command => \&edit_paste ], [Button => 'Delete', -command => \&edit_delete ], [Separator => ''], [Cascade => 'Object ...', -tearoff => 0, -menuitems => [ [ Button => "Circle", -command => \&edit_circle ], [ Button => "Square", -command => \&edit_square ], [ Button => "Point", -command => \&edit_point ] ] ], ])->grid(-row => 0, -column => 0, -sticky => 'w');<ACLASS="indexterm"NAME="ch15-idx-1000005166-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005166-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005166-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005166-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005166-4"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1581">See Also</A></H3><PCLASS="para">The documentation for the Tk module from CPAN</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_14.htm"TITLE="15.13. Controlling Another Program with Expect"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.13. Controlling Another Program with Expect"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_16.htm"TITLE="15.15. Creating Dialog Boxes with Tk"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.15. Creating Dialog Boxes with Tk"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">15.13. Controlling Another Program with Expect</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.15. Creating Dialog Boxes with Tk</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 + -