⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch21_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>Perl/Tk (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 &amp; 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="part9.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 21. Perl/Tk</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4>  <p> <a href="#perlnut2-CHP-21-SECT-1">Widgets</a><br /><a href="ch21_02.htm">Geometry Managers</a><br /><a href="ch21_03.htm">Common Widget Configuration Options</a><br /><a href="ch21_04.htm">The Button Widget</a><br /><a href="ch21_05.htm">The Checkbutton Widget</a><br /><a href="ch21_06.htm">The Radiobutton Widget</a><br /><a href="ch21_07.htm">The Label Widget</a><br /><a href="ch21_08.htm">The Entry Widget</a><br /><a href="ch21_09.htm">The Scrollbar Widget</a><br /><a href="ch21_10.htm">The Listbox Widget</a><br /><a href="ch21_11.htm">The Text Widget</a><br /><a href="ch21_12.htm">The Canvas Widget</a><br /><a href="ch21_13.htm">The Scale Widget</a><br /><a href="ch21_14.htm">The Menubutton Widget</a><br /><a href="ch21_15.htm">The Menu Widget</a><br /><a href="ch21_16.htm">The Optionmenu Widget</a><br /><a href="ch21_17.htm">The Frame Widget</a><br /><a href="ch21_18.htm">The Toplevel Widget</a><br /></p></div><p>Perl/Tk is an extension for writing Perl programs with a graphicaluser interface (GUI) on both Unix and Windows 95/NT. Tk wasoriginally developed as an extension to the Tcl language, for usewith the X Window System on Unix. With its port to Perl, Tk givesPerl programmers the same control over the graphical desktop that Tclprogrammers have taken for granted<a name="INDEX-2668" /></a><a name="INDEX-2669" /></a>.</p><p>The Tk extension makes it easy to draw a window, put widgets into it(such as buttons, checkboxes, entry fields, menus, etc.), and havethem perform certain actions based on user input. A simple"Hello World" program would looklike this:</p><blockquote><pre class="code">#!/usr/bin/perl -wuse Tk;my $mw = MainWindow-&gt;new;$mw-&gt;Button(-text =&gt; "Hello World!", -command =&gt;sub{exit})-&gt;pack;MainLoop;</pre></blockquote><p>When you run it, it would look like <a href="ch21_01.htm#perlnut2-CHP-21-FIG-1">Figure 21-1</a>.</p><a name="perlnut2-CHP-21-FIG-1" /></a><div class="figure"><img src="figs/pns2_2101.gif" alt="Figure 21-1" width="146" height="55" /></div><h4 class="objtitle">Figure 21-1. A simple Perl/Tk program</h4><p>Clicking on the Hello World button exits the program, and your windowdisappears.</p><p>Let's walk through these few lines of code. Aftercalling the Perl interpreter, the program calls the Tk module. Itthen proceeds to build a generic, standard window(<tt class="literal">MainWindow</tt>) to act as a parent for any otherwidgets you create. Line 4 of the program creates a button anddisplays it using the <tt class="literal">pack</tt> geometry manager. Italso gives the button something to do when pushed (in this case, exitthe program).</p><p><a name="INDEX-2670" /></a>The last line tells the program to"go do it."<tt class="literal">MainLoop</tt> starts the event handler for thegraphical interface, and the program draws windows until it reachesthe <tt class="literal">MainLoop</tt> statement. Everything up to thatpoint is preparation; until you reach the <tt class="literal">MainLoop</tt>statement, the program simply prepares its windows and defines whatto do when certain events happen (such as a mouse click on the HelloWorld! button). Nothing is drawn until the<tt class="literal">MainLoop</tt> statement is reached.</p><div class="sect1"><a name="perlnut2-CHP-21-SECT-1" /></a><h2 class="sect1">21.1. Widgets</h2><p><a name="INDEX-2671" /></a><a name="INDEX-2672" /></a><a name="INDEX-2673" /></a>Widgetsin Perl/Tk are created with <em class="emphasis">widget creationcommands</em>, which include <tt class="literal">Button</tt>,<tt class="literal">Canvas</tt>, <tt class="literal">CheckButton</tt>,<tt class="literal">Entry</tt>, <tt class="literal">Frame</tt>,<tt class="literal">Label</tt>, <tt class="literal">Listbox</tt>,<tt class="literal">Menu</tt>, <tt class="literal">Menubutton</tt>,<tt class="literal">Message</tt>, <tt class="literal">Radiobutton</tt>,<tt class="literal">Scale</tt>, <tt class="literal">Scrollbar</tt>,<tt class="literal">Text</tt>, and <tt class="literal">Toplevel</tt>, among many,many others.</p><p>Positioning widgets is done with <em class="emphasis">geometrymanagers</em>. In the "HelloWorld" example shown earlier, the<tt class="literal">pack</tt> command is the geometry manager. Geometrymanagers determine where in the window (or frame) the widget willsit. We'll talk more about the Perl/Tk geometrymanagers later in this chapter.</p><a name="perlnut2-CHP-21-SECT-1.1" /></a><div class="sect2"><h3 class="sect2">21.1.1. Widget Methods</h3><p><a name="INDEX-2674" /></a><a name="INDEX-2675" /></a><a name="INDEX-2676" /></a><a name="INDEX-2677" /></a>Widgets can be configured, queried, ormanipulated via various <em class="emphasis">widget methods</em>. Forexample, all widgets support the <tt class="literal">configure</tt> widgetmethod for changing widget properties after the widget is created. Inaddition, most widgets have specialized methods associated with themfor manipulating the widget as needed throughout the program. Forexample, widgets that scroll support the <tt class="literal">xview</tt> and<tt class="literal">yview</tt> methods for determining the viewable portionof the content when the scrollbar is moved. The Entry and Textwidgets have methods for inserting and deleting values. The Canvaswidget has a whole series of methods for drawing shapes and insertingtext into the canvas. And so on.</p><p>Widget methods are listed in the discussion of each widget later inthis chapter. However, since all widgets support the<tt class="literal">configure</tt> and <tt class="literal">cget</tt> methods, wewill cover them now.</p><a name="perlnut2-CHP-21-SECT-1.1.1" /></a><div class="sect3"><h3 class="sect3">21.1.1.1. The configure method</h3><p><a name="INDEX-2678" /></a>The <tt class="literal">configure</tt>method can be used to set and retrieve widget configuration values.For example, to change the width of a button:</p><blockquote><pre class="code">$button-&gt;configure(-width =&gt; 100);</pre></blockquote><p>To get the value for a current widget, just supply it without avalue:</p><blockquote><pre class="code">$button-&gt;configure(-width);</pre></blockquote><p>The result is an array of scalars; the important values are the lasttwo, which represent the default value and its current value,respectively.</p><p>You can also call <tt class="literal">configure</tt> without any options atall, which will give you a listing of all options and their values.</p></div>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -