Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 2361

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>5.9 Listboxes and Scrollbars</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node27.html#section.widgets-2.scale">&lt;&lt; Prev</A></TD><TD><A href="node19.html">- Up -</A></TD><TD><A href="node29.html#section.widgets-2.wm">Next &gt;&gt;</A></TD></TR></TABLE><DIV id="section.widgets-2.listbox"><H2><A name="section.widgets-2.listbox">5.9 Listboxes and Scrollbars</A></H2><DIV class="apropos"><P class="margin">scanning</P><P> A listbox displays a list of strings and allows the user to select one or more of them. If the listbox contains more lines than it can display at once, the user can select the strings to be displayed by <A name="label247"></A><EM>scanning</EM> the listbox. The listbox can be scanned by pressing the second mouse button and moving the mouse pointer up or down while the button is still being pressed. </P></DIV><P> A more convenient way than scanning is to use <A name="label248"></A><EM>scrollbar</EM> widgets. A scrollbar widgets allows the user to determine the portion of strings displayed by moving a slider. Scrollbars are independent of a particular widget type: they can be also attached to other widgets including entries. </P><P> <A href="node28.html#figure.widgets-2.listbox">Figure&nbsp;5.10</A> shows a program that allows to select a color from a list of colors. The list of colors is provided by some external file inserted at the beginning of the program. The listbox object is initialized and creates an event binding for pressing the left mouse button as follows. First the currently selected index&nbsp;<CODE>I</CODE> is retrieved (the strings in the list box are indexed). Then the string&nbsp;<CODE>C</CODE> at this index is retrieved and used as background color of the listbox widget. </P><DIV id="figure.widgets-2.listbox"><HR><P><A name="figure.widgets-2.listbox"></A></P><P> </P><DIV align="center"><IMG alt="" src="listbox.gif"></DIV><P> <A name="label250"></A> <A name="label252"></A> <A name="label254"></A> <A name="label256"></A> </P><DL class="anonymous"><DD class="code"><CODE>L={New&nbsp;Tk<SPAN class="keyword">.</SPAN>listbox&nbsp;tkInit(parent:W&nbsp;height:6)}<BR>{L&nbsp;tkBind(event:&nbsp;&nbsp;<SPAN class="string">'&lt;1&gt;'</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action:&nbsp;<SPAN class="keyword">proc</SPAN><SPAN class="variablename">&nbsp;</SPAN>{<SPAN class="functionname">$</SPAN>}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I={L&nbsp;tkReturn(curselection&nbsp;$)}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C={L&nbsp;tkReturn(get(I)&nbsp;$)}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">in</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{L&nbsp;tk(configure&nbsp;bg:C)}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>)}<BR>S={New&nbsp;Tk<SPAN class="keyword">.</SPAN>scrollbar&nbsp;tkInit(parent:W)}<BR>{ForAll&nbsp;</CODE><SPAN class="chunktitle"><SPAN class="chunkborder">&lt;</SPAN><A href="node46.html#label345">Color names</A><SPAN class="chunkborder">&gt;</SPAN></SPAN><CODE>&nbsp;&nbsp;<BR>&nbsp;<SPAN class="keyword">proc</SPAN><SPAN class="variablename">&nbsp;</SPAN>{<SPAN class="functionname">$</SPAN>&nbsp;C}<BR>&nbsp;&nbsp;&nbsp;&nbsp;{L&nbsp;tk(insert&nbsp;<SPAN class="string">'end'</SPAN>&nbsp;C)}<BR>&nbsp;<SPAN class="keyword">end</SPAN>}<BR>{Tk<SPAN class="keyword">.</SPAN>addYScrollbar&nbsp;L&nbsp;S}<BR>{Tk<SPAN class="keyword">.</SPAN>send&nbsp;pack(L&nbsp;S&nbsp;fill:y&nbsp;side:left)}</CODE></DD></DL><P> </P><P class="caption"><STRONG>Figure&nbsp;5.10:</STRONG> A listbox together with a vertical scrollbar.</P><HR></DIV><P> </P><DIV class="apropos"><P class="margin">attaching scrollbars</P><P> To attach a scrollbar to a widget we use the predefined procedure <CODE>Tk<SPAN class="keyword">.</SPAN>addYScrollbar</CODE>. It creates event bindings for the scrollbar such that moving the scrollbar's slider affects the visible strings of the listbox. Also it creates event bindings for the listbox such that scanning the listbox is reflected by the scrollbar. </P></DIV><P> More information on listboxes can be found in <A href="../tcltk/TkCmd/listbox.htm"><KBD>listbox</KBD></A> and more information on scrollbars in <A href="../tcltk/TkCmd/scrollbar.htm"><KBD>scrollbar</KBD></A>. </P></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node27.html#section.widgets-2.scale">&lt;&lt; Prev</A></TD><TD><A href="node19.html">- Up -</A></TD><TD><A href="node29.html#section.widgets-2.wm">Next &gt;&gt;</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~schulte/">Christian&nbsp;Schulte</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>