📄 ch21_10.htm
字号:
<html><head><title>The Listbox Widget (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch21_09.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch21_11.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">21.10. The Listbox Widget</h2><p><a name="INDEX-2924" /><a name="INDEX-2925" /><a name="INDEX-2926" />Create a listbox with the<tt class="literal">Listbox</tt> method. You can then insert items into thelistbox using the <tt class="literal">insert</tt> method.</p><blockquote><pre class="code">$parentwidget->Listbox (<em class="replaceable"><tt>options</tt></em>)</pre></blockquote><p>The standard configuration options that apply to<tt class="literal">Listbox</tt> are: <tt class="literal">-background</tt>,<tt class="literal">-bg</tt>, <tt class="literal">-borderwidth</tt>,<tt class="literal">-bd</tt>, <tt class="literal">-cursor</tt>,<tt class="literal">-exportselection</tt>, <tt class="literal">-font</tt>,<tt class="literal">-foreground</tt>, <tt class="literal">-fg</tt>,<tt class="literal">-height</tt>, <tt class="literal">-highlightbackground</tt>,<tt class="literal">-highlightcolor</tt>,<tt class="literal">-highlightthickness</tt>, <tt class="literal">-relief</tt>,<tt class="literal">-select</tt>-<tt class="literal">background</tt>,<tt class="literal">-selectborderwidth</tt>,<tt class="literal">-selectforeground</tt>, <tt class="literal">-takefocus</tt>,<tt class="literal">-width</tt>,<tt class="literal">-xscroll</tt>-<tt class="literal">command</tt>, and<tt class="literal">-yscrollcommand</tt>.</p><p>Other options are:</p><dl><dt><b><tt class="literal">-selectmode =></tt> <em class="replaceable">mode</em></b></dt><dd><a name="INDEX-2927" />Determines the number of itemsthat can be selected at once, as well as key/mouse bindings.<em class="replaceable"><tt>mode</tt></em> can be any of:</p><dl><dt><i> <tt class="literal">single</tt> </i></dt><dd>Only one item can be selected at a time.</p></dd><dt><i> <tt class="literal">browse</tt></i></dt><dd>Only one item can be selected at a time (default).</p></dd><dt><i> <tt class="literal">multiple</tt></i></dt><dd>Multiple items can be selected at a time.</p></dd><dt><i> <tt class="literal">extended</tt></i></dt><dd>Multiple items can be selected at a time.</p></dd></dl></dd><dt><b><tt class="literal">-setgrid =></tt> <em class="replaceable">boolean</em></b></dt><dd><a name="INDEX-2928" />Turns on gridding for the listbox. Ifthe widget is resized, only complete lines and characters aredisplayed. Default is <tt class="literal">0</tt> (off).</p></dd></dl><a name="perlnut2-CHP-21-SECT-10.1" /><div class="sect2"><h3 class="sect2">21.10.1. Listbox Indexes</h3><p>In a Listbox widget, several indexes are defined to identifypositions in the listbox. These are used by the methods that retrieveand manipulate listbox entries.</p><dl><dt><i><em class="replaceable"><tt>n</tt></em></i></dt><dd><a name="INDEX-2929" />An integer representing a position inthe list, with <tt class="literal">0</tt> as the first item.</p></dd><dt><b><tt class="literal">active</tt></b></dt><dd><a name="INDEX-2930" />The item with the keyboard focus.</p></dd><dt><b><tt class="literal">anchor</tt></b></dt><dd><a name="INDEX-2931" />The anchored position.</p></dd><dt><b><tt class="literal">end</tt></b></dt><dd><a name="INDEX-2932" />The last element in the listbox.</p></dd><dt><b><tt class="literal">@</tt><em class="replaceable">x,y</em></b></dt><dd>The listbox item containing the specified x,y coordinate.</p></dd></dl></div><a name="perlnut2-CHP-21-SECT-10.2" /><div class="sect2"><h3 class="sect2">21.10.2. Listbox Methods</h3><p>In addition to <tt class="literal">configure</tt> and<tt class="literal">cget</tt>, the following methods are supported byListbox:</p><dl><dt><b><tt class="literal">insert</tt></b></dt><dd><a name="INDEX-2933" />Adds items to a listbox at thespecified index. For example, to insert items at the end of a list:</p><blockquote><pre class="code">$listbox->insert('end', "Puerto Rico", "Virgin Islands", "Guam");</pre></blockquote></dd><dt><b><tt class="literal">delete</tt></b></dt><dd><a name="INDEX-2934" />Deletes items from a listbox. To deleteall items:</p><blockquote><pre class="code">$listbox->delete(0,'end');</pre></blockquote></dd><dt><b><tt class="literal">get</tt></b></dt><dd><a name="INDEX-2935" />Returns a list of elements within thespecified range of indexes. For a list of all elements:</p><blockquote><pre class="code">@items = $listbox->get(0,'end');</pre></blockquote></dd><dt><b><tt class="literal">curselection</tt></b></dt><dd><a name="INDEX-2936" />Returns a list of all currentlyselected elements in the listbox.</p></dd><dt><b><tt class="literal">activate</tt></b></dt><dd><a name="INDEX-2937" />Sets the specified item to be theactive element.</p></dd><dt><b><tt class="literal">bbox</tt></b></dt><dd><a name="INDEX-2938" />Returns the location and dimensions ofthe bounding box surrounding the specified listbox element. Thereturned list contains four numbers representing (respectively) the xcoordinate of the upper-left corner, the y coordinate of theupper-left corner, the width of the text in pixels, and the height ofthe text in pixels.</p></dd><dt><b><tt class="literal">index</tt></b></dt><dd><a name="INDEX-2939" />Converts a named index into a numericone.</p></dd><dt><b><tt class="literal">nearest</tt></b></dt><dd><a name="INDEX-2940" />Gets the index of the listbox itemnearest a given y coordinate.</p></dd><dt><b><tt class="literal">see</tt></b></dt><dd><a name="INDEX-2941" />Pages the listbox up or down to displaythe specified item.</p></dd><dt><b><tt class="literal">selection</tt></b></dt><dd><a name="INDEX-2942" />Manipulates the selected block of listitems. The first argument can be any of:</p><dl><dt><i> <tt class="literal">anchor</tt> </i></dt><dd>Sets the anchor index to the specified index.</p></dd><dt><i> <tt class="literal">clear</tt></i></dt><dd>Clears any selected list items in the specified range. For example,to clear all selections:</p><blockquote><pre class="code">$listbox->selection('clear', 0, 'end');</pre></blockquote></dd><dt><i> <tt class="literal">includes</tt></i></dt><dd>Returns <tt class="literal">0</tt> or <tt class="literal">1</tt> depending onwhether the specified item is already selected:</p><blockquote><pre class="code">$listbox->selection('includes', 'end');</pre></blockquote></dd><dt><i> <tt class="literal">set</tt></i></dt><dd>Selects a range of items in the listbox:</p><blockquote><pre class="code">$listbox->selection('set', 0, 'end');</pre></blockquote></dd></dl></dd><dt><b><tt class="literal">size</tt></b></dt><dd><a name="INDEX-2943" />Returns the total number of items inthe listbox.</p></dd><dt><b><tt class="literal">xview</tt></b></dt><dd><a name="INDEX-2944" />Manipulates the text in view. With noarguments, returns a list of two numbers between 0 and 1, definingthe portion of the list text that is currently hidden on the left andright sides, respectively. With arguments, the function of<tt class="literal">xview</tt> changes:</p><dl><dt><i><em class="replaceable"><tt>index</tt></em></i></dt><dd>If the first argument is an index, that position becomes the leftmostposition in view.</p></dd><dt><i> <tt class="literal">moveto</tt> </i></dt><dd>Moves the specified fraction of the listbox to the left of thevisible portion.</p></dd><dt><i> <tt class="literal">scroll</tt></i></dt><dd>Scrolls the text left or right by the specified number of units(characters, in this context) or pages. Used primarily as a callbackto a scrollbar; pressing on an arrow would move by units(characters), and pressing on the trough would move by pages. Thenumber is either <tt class="literal">1</tt> or <tt class="literal">-1</tt>, tomove forwards or backwards, respectively.</p></dd></dl></dd><dt><b><tt class="literal">yview</tt></b></dt><dd><a name="INDEX-2945" />Manipulates the text in view. With noarguments, returns a list of two numbers between 0 and 1, definingthe portion of the list text that is currently hidden on the top andbottom, respectively. With arguments, the function of<tt class="literal">yview</tt> changes:</p><dl><dt><i> <em class="replaceable"><tt>index</tt></em></i></dt><dd>If the first argument is an index, that position becomes the topmostposition in view.</p></dd><dt><i> <tt class="literal">moveto</tt> </i></dt><dd>Moves the specified fraction of the listbox to the top of the visibleportion.</p></dd><dt><i> <tt class="literal">scroll</tt></i></dt><dd>Scrolls the text up or down by the specified number of units (lines,in this context) or pages. Used primarily as a callback to ascrollbar; pressing on an arrow would move by units (lines), andpressing on the trough would move by pages. The number is either<tt class="literal">1</tt> or <tt class="literal">-1</tt>, to move forwards orbackwards, respectively.</p></dd></dl></dd></dl></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch21_09.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch21_11.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">21.9. The Scrollbar Widget</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">21.11. The Text Widget</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><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 + -