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

📄 ch15_17.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 15.16. Responding to Tk Resize Events (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:43:31Z"><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_16.htm"TITLE="15.15. Creating Dialog Boxes with Tk"><LINKREL="next"HREF="ch15_18.htm"TITLE="15.17. Removing the DOS Shell Window with Windows Perl/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_16.htm"TITLE="15.15. Creating Dialog Boxes with Tk"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.15. Creating Dialog Boxes 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_18.htm"TITLE="15.17. Removing the DOS Shell Window with Windows Perl/Tk"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.17. Removing the DOS Shell Window with Windows Perl/Tk"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch15-chap15_responding_0">15.16. Responding to Tk Resize Events</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1857">Problem<ACLASS="indexterm"NAME="ch15-idx-1000005195-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005195-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005195-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005195-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005195-4"></A><ACLASS="indexterm"NAME="ch15-idx-1000005195-5"></A></A></H3><PCLASS="para">You've written a Tk program, but your widget layout goes awry when the user resizes their window.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1863">Solution</A></H3><PCLASS="para">You can prevent the user from resizing the window by intercepting the Configure event:</P><PRECLASS="programlisting">use Tk;$main = MainWindow-&gt;new();$main-&gt;bind('&lt;Configure&gt;' =&gt; sub {    $xe = $main-&gt;XEvent;    $main-&gt;maxsize($xe-&gt;w, $xe-&gt;h);    $main-&gt;minsize($xe-&gt;w, $xe-&gt;h);});</PRE><PCLASS="para">Or you can use <CODECLASS="literal">pack</CODE> to control how each widget resizes and expands when the user resizes its container:</P><PRECLASS="programlisting">$widget-&gt;pack( -fill =&gt; &quot;both&quot;, -expand =&gt; 1 );$widget-&gt;pack( -fill =&gt; &quot;x&quot;,    -expand =&gt; 1 );</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1893">Discussion</A></H3><PCLASS="para">By default, packed widgets resize if their container changes size &nbsp;-  they don't scale themselves or their contents to the new size. This can lead to empty space between widgets, or cropped or cramped widgets if the user resizes the window.</P><PCLASS="para">One solution is to prevent resizing. We <CODECLASS="literal">bind</CODE> to the <CODECLASS="literal">&lt;Configure&gt;</CODE> event, which is sent when a widget's size or position changes, registering a callback to reset the window's size. This is how you'd ensure a popup error-message box couldn't be resized.</P><PCLASS="para">You often want to let the user resize the application's windows. You must then define how each widget will react. Do this through the arguments to the <CODECLASS="literal">pack</CODE> method: <CODECLASS="literal">-fill</CODE> controls the dimensions the widget will resize in, and <CODECLASS="literal">-expand</CODE> controls whether the widget's size will change to match available space. The <CODECLASS="literal">-expand</CODE> option takes a Boolean value, true or false. The <CODECLASS="literal">-fill</CODE> option takes a string indicating the dimensions the widget can claim space in: <CODECLASS="literal">&quot;x&quot;</CODE>, <CODECLASS="literal">&quot;y&quot;</CODE>, <CODECLASS="literal">&quot;both&quot;</CODE>, or <CODECLASS="literal">&quot;none&quot;</CODE>.</P><PCLASS="para">The solution requires both options. Without <CODECLASS="literal">-fill</CODE>, <CODECLASS="literal">-expand</CODE> won't claim space to grow into. Without <CODECLASS="literal">-expand </CODE>, <CODECLASS="literal">-fill</CODE> will claim empty space but won't expand in it.</P><PCLASS="para">Different parts of your application will behave differently. The main area of a web browser, for example, should probably change size in both dimensions when the window is resized. You'd pack the widget like this:</P><PRECLASS="programlisting">$mainarea-&gt;pack( -fill =&gt; &quot;both&quot;, -expand =&gt; 1);</PRE><PCLASS="para">The menubar above the main area, though, should expand horizontally but not vertically. You'd pack the widget thus:</P><PRECLASS="programlisting">$menubar-&gt;pack( -fill =&gt; &quot;x&quot;, -expand =&gt; 1 );</PRE><PCLASS="para">Associated with resizing is the need to anchor a widget to part of its container. Here's how you'd anchor the menubar to the top left corner of its container when you call <CODECLASS="literal">pack</CODE>:</P><PRECLASS="programlisting">$menubar-&gt;pack (-fill     =&gt; &quot;x&quot;,                -expand   =&gt; 1,                -anchor   =&gt; &quot;nw&quot; );</PRE><PCLASS="para">Now when you resize it, the menubar stays at the top of the window where it belongs, instead of being centered in wide open space.<ACLASS="indexterm"NAME="ch15-idx-1000005200-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005200-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005200-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005200-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005200-4"></A><ACLASS="indexterm"NAME="ch15-idx-1000005200-5"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1923">See Also</A></H3><PCLASS="para">The <ICLASS="filename">pack </I>(n), <ICLASS="filename">XEvent </I>(3), and <ICLASS="filename">XConfigureEvent </I>(3) manpages (if you have them); <CITECLASS="citetitle">Tcl and the Tk Toolkit</CITE>, by John Ousterhout, Addison-Wesley (1994)</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_16.htm"TITLE="15.15. Creating Dialog Boxes with Tk"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.15. Creating Dialog Boxes 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_18.htm"TITLE="15.17. Removing the DOS Shell Window with Windows Perl/Tk"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.17. Removing the DOS Shell Window with Windows Perl/Tk"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">15.15. Creating Dialog Boxes 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.17. Removing the DOS Shell Window with Windows Perl/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 &copy; 2002</a> O'Reilly &amp; 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 + -