209.html

来自「Python Ebook Python&XML」· HTML 代码 · 共 285 行 · 第 1/2 页

HTML
285
字号
						
					</CEnter>

					<P>OK, now let's see what is really happening in both examples. First, we have to import the <TT Class="monofont">Tkinter</TT> module. Then, we have to create a widget to hold the other objects. (In the first example we used a top-level window by calling the <A Name="idx1073748363"></a>
						<A NAMe="idx1073748364"></a>
						<tt class="monofont">Tk()</tt> method and in the second one we used an instance of the <tt class="monofont">Toplevel</tt> widget). Creating another <Tt cLass="monofont">Tk()</Tt> instance is really starting up another instance of Tk梚t is less expensive to create a <tt cLass="monofont">Toplevel.</TT>
					</P>

					<P>When we have a background, we can start adding widgets to it. In the first example, we added a single button, and in the second one, we added two widgets: a label and a button.</p>

					<p>You don't necessarily <a nAME="idx1073748365"></A>
						<a namE="idx1073748366"></A>
						<A Name="idx1073748367"></a>associate a widget with its toplevel梱ou associate it with its parent widget (if you are adding a Button to a Frame that is a child of a Toplevel, you would pass the frame as the first argument). It is also necessary to <A NAMe="idx1073748368"></a>
						<a name="idx1073748369"></a>pack the widgets in order to display them in the window. This last process is part of a concept called <a name="idx1073748370"></a>geometry management, which is used to manage the position and layout of widgets.<a name="idx1073748371"></a>
						<A naMe="idx1073748372"></a>
						<a Name="idx1073748373"></a>
						<A namE="idx1073748374"></A>
						<A Name="idx1073748375"></a>
						<A NAMe="idx1073748376"></a>
						<a nAME="idx1073748377"></A>
						<a namE="idx1073748378"></A>
					</P>

					<P>Both examples demonstrate how you can bind actions to the events that can occur in your widget. Here, we are using a specific <a name="idx1073748379"></a>
						<a name="idx1073748380"></a>attribute called <i>command</i> for this purpose. You can use keyword arguments to transport several attributes and their values to a widget, which is a simple way to handle multiple attributes.</p>

					<p>The last thing that we need to do is to start an <a namE="idx1073748381"></a>
						<a Name="idx1073748382"></A>
						<a namE="idx1073748383"></a>event loop. Note that the application only appears when you start the loop engine. Regular Python scripts are executed top to bottom, and when the last line of code is executed, the program quits. We don't want this kind of behavior in a GUI application. Therefore, we need to call the <a nAME="idx1073748384"></A>
						<a namE="idx1073748385"></A>
						<TT clasS="monofont">mainloop()</TT> method of the top-level window of our application. This method keeps the GUI indefinitely running until the window is closed. This loop is responsible for redrawing the window widgets whenever it becomes necessary, for handling events (such as key presses and mouse clicks), and for managing Tkinter operations, such as all the geometry management functions.<A name="idx1073748386"></A>
						<A NAme="idx1073748387"></a>
						<a name="idx1073748388"></a>
						<a name="idx1073748389"></a>
						<a name="idx1073748390"></A>
						<a nAme="idx1073748391"></a>
						<A name="idx1073748392"></A>
						<a naME="idx1073748393"></A>
					</P>

					<p>Note that the version of Tkinter<a naME="idx1073748394"></A>
						<A name="idx1073748395"></A>
						<A NAme="idx1073748396"></a>
						<a NAME="idx1073748397"></a> shipped along with Python 2.0 also provides support for Unicode characters because Tkinter is now able to display Unicode string in Tk widgets. Talking about Python 2.0, Tkinter had some optimizations done in order to make some operations much faster. It is also good to mention that the support for Tcl/Tk 7.X versions has been dropped in this latest release.</p>

				
			</font>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 1/30/2002<br>Python Developer's Handbook, &copy;&nbsp;2002 Sams Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="208.html" title="Introduction to Tcl/Tk"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=209" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="209.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="210.html" title="Geometry Management"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
</TD></TR></TABLE>
<br><TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD><H4 class=Title>Index terms contained in this section</H4>
<font size=2>
(from Tkinter import *) module<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748362">loading</a><BR>
 <a href="#idx1073748352">.pyw extension</a><BR>
 <a href="#idx1073748339">_tkinter module</a><BR>
applications<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello World<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748376">Tkinter</a> <a href="#idx1073748391">2nd</a><BR>
associating<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748365">widgets with toplevels</a><BR>
attributes<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748380">command</a><BR>
characters<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Unicode<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748397">Tkinter module</a><BR>
 <a href="#idx1073748379">command attribute</a><BR>
event loops<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748382">starting</a><BR>
extensions<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748353">.pyw</a><BR>
files<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;saving<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748350">Tkinter</a><BR>
 <a href="#idx1073748345">fixtk utility</a><BR>
 <a href="#idx1073748370">geometry management</a><BR>
graphical user interfaces (GUIs)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748334">Tkinter</a> <a href="#idx1073748342">2nd</a> <a href="#idx1073748357">3rd</a> <a href="#idx1073748372">4th</a> <a href="#idx1073748387">5th</a><BR>
Hello World program<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748375">Tkinter</a> <a href="#idx1073748390">2nd</a><BR>
installing<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748348">Tkinter</a><BR>
interfaces<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;graphical user (GUI)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748335">Tkinter</a> <a href="#idx1073748343">2nd</a> <a href="#idx1073748358">3rd</a> <a href="#idx1073748373">4th</a> <a href="#idx1073748388">5th</a><BR>
loading<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748360">(from Tkinter import *) module</a><BR>
loops<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748383">starting</a><BR>
 <a href="#idx1073748332">Lumholdt, Steen</a><BR>
 <a href="#idx1073748384">mainloop() method</a><BR>
methods<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748385">mainloop()</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748355">Tk()</a> <a href="#idx1073748364">2nd</a><BR>
modules<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(from Tkinter import *)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748361">loading</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748340">_tkinter</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748336">Tkinter</a> <a href="#idx1073748344">2nd</a> <a href="#idx1073748359">3rd</a> <a href="#idx1073748374">4th</a> <a href="#idx1073748389">5th</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748395">Unicode characters</a><BR>
packing<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748368">widgets</a><BR>
programs<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello World<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748377">Tkinter</a> <a href="#idx1073748392">2nd</a><BR>
saving files<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748349">Tkinter</a><BR>
software<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello World<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748378">Tkinter</a> <a href="#idx1073748393">2nd</a><BR>
starting<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748381">event loops</a><BR>
 <a href="#idx1073748354">Tk() method</a> <a href="#idx1073748363">2nd</a><BR>
 <a href="#idx1073748333">Tkinter module</a> <a href="#idx1073748341">2nd</a> <a href="#idx1073748356">3rd</a> <a href="#idx1073748371">4th</a> <a href="#idx1073748386">5th</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748394">Unicode characters</a><BR>
tools<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748347">fixtk</a><BR>
toplevels<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748367">associating widgets with</a><BR>
Unicode characters<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748396">Tkinter module</a><BR>
UNIX<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748338">installing Tkinter</a><BR>
utilities<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748346">fixtk</a><BR>
widgets<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748366">associating with toplevels</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748369">packing</a><BR>
Windows<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748337">installing Tkinter</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748351">saving files, Tkinter</a><BR>
<BR>
</font></TD></TR></TABLE>




<!--EndOfBrowse-->

</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>

⌨️ 快捷键说明

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