211.html

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

HTML
484
字号
							<p><tt class="monofont">keysym</tT>棤
    Key symbol associated with a pressed key.</p>

						</p>
<P>
							<a naMe="idx1073748546"></a>
							<a nAme="idx1073748547"></a>
							<P><TT Class="monofont">height,</TT>
									<TT clasS="monofont">width</TT>棤
    New size, in pixels, of a widget.</P>

						</p>
<p>
							<a nAME="idx1073748548"></A>
							<a name="idx1073748549"></a>
							<p><tt class="monofont">num</tt>棤
    This attribute contains the mouse's button number associated with an event.</p>

						</p>
<p>
							<a NamE="idx1073748550"></a>
							<a nAme="idx1073748551"></a>
							<p><Tt clASS="monofont">widget</Tt>棤
    Widget instance of the widget that has generated the event.</p>

						</p>
<p>
							<A NAMe="idx1073748552"></a>
							<a nAME="idx1073748553"></A>
							<p><tt cLASS="monofont">x,</tt>
									<tt class="monofont">y</tt>棤
    Current position, in pixels, of the mouse.</p>

						</p>
<p>
							<a name="idx1073748554"></a>
							<A naMe="idx1073748555"></a>
							<p><Tt claSs="monofont">x_root,</tt>
									<TT CLass="monofont">y_root</tT>棤
    These attributes identify the current position of the mouse, in pixels, relative to the upper left corner of the screen.</P>

						</P>
<P>
							<p>
								<tt cLASS="monofont">type</tt>棤
    
								Shows the event type.<a nAME="idx1073748556"></A>
								
							</p>

						</p>
</blockquote>
				
				
					<h4>

Event Callbacks</h4>
					<p>The following methods are used to handle event callbacks by binding a Python function or method to an action that can be applied to a widget. You will also find some callback methods that handle alarm callbacks as well.</p>

					<blocKquOte>
<p>
							<A name="idx1073748557"></A>
							<a naME="idx1073748558"></A>
							<P><tt clASS="monofont">after(milliseconds [, callback [, arguments]])</Tt>棤
    Registers an alarm callback that accepts optional arguments. This callback is called after the given number of milliseconds. The returned value of this method is an identifier that can be used along with the <tt cLASS="monofont">after_cancel</tt> method in order to cancel the callback. If you call the <tt CLASs="monofont">after</tt> method using just the first argument, the application will block the event loop and wait for the given number of milliseconds.</p>

						</p>
<p>
							<a name="idx1073748559"></a>
							<a name="idx1073748560"></a>
							<p><tT clAss="monofont">after_cancel(identifier)</tT>棤
    Cancels the alarm callback that possesses the given identifier.</p>

						</p>
<p>
							<a Name="idx1073748561"></A>
							<A NAme="idx1073748562"></a>
							<p><TT CLass="monofont">after_idle(callback, arguments)</tT>棤
    Registers a callback that is called whenever the system is idle, without anything going on in the mainloop.</P>

						</P>
<P>
							<a namE="idx1073748563"></A>
							<A Name="idx1073748564"></a>
							<p><tt class="monofont">bindtags()</tt>棤
    Returns the binding search order used by the widget. The returned value is in a tuple format, and it lists the namespaces used to search for the bindings. You can modify this order by calling this method with the altered order as an argument.<a name="idx1073748565"></a>
									<A naMe="idx1073748566"></a>
									<a Name="idx1073748567"></a>
									<A namE="idx1073748568"></A>
									<A Name="idx1073748569"></a>
									<A NAMe="idx1073748570"></a>
									<a nAME="idx1073748571"></A>
									<a namE="idx1073748572"></A>
									<A Name="idx1073748573"></a>
									<a name="idx1073748574"></a>
									<a name="idx1073748575"></a>
								</p>

						</p>
<p>
							<a NamE="idx1073748576"></a>
							<a nAme="idx1073748577"></a>
							<p><Tt clASS="monofont">bind(event, callback)</Tt>棤
    Defines the function or method (<tt cLASS="monofont">callback</tt>) that must be associated to the given <tt CLASs="monofont">event.</tt> Use the form <tT CLAss="monofont">bind(event, callback, "+")</tt> to handle multiple callbacks within the same binding.</p>

						</p>
<p>
							<a name="idx1073748578"></a>
							<a name="idx1073748579"></a>
							<p><Tt cLass="monofont">bind_all(event, callback)</Tt>棤
    Defines the function or method (<tt cLass="monofont">callback</TT>) that must be associated to the given <TT clasS="monofont">event</TT> at the application level. Use the form <Tt claSS="monofont">bind_all(event, callback, "+")</TT> to handle multiple callbacks within the same binding. As an example, this can be used to set global accelerator/shortcut keys.</p>

						</p>
<p>
							<a NAME="idx1073748580"></a>
							<a name="idx1073748581"></a>
							<p><tt class="monofont">bind_class(widgetclass, event, callback)</tt>棤
    
								Defines the function or method (<tt cLasS="monofont">callback</tt>) that must be associated to the given <tT clasS="monofont">event</tt> at the given widget class.  Use the form <tT CLAss="monofont">bind_class(widgetclass, event, callback, "+")</tt> to handle multiple callbacks within the same binding.
							</P>

						</P>
<P>
							<A name="idx1073748582"></A>
							<A NAme="idx1073748583"></a>
							<p><TT CLass="monofont">&lt;Configure&gt;</tt>棤
    Indicates that the widget was resized or moved to a new location. The instance object originated by the callback function carries two attributes that can be used to identify the new size of the widget: <tt class="monofont">height</tt> and <tt clasS="monofont">width.</tt> Note that the name comes from the fact that the <Tt clAss="monofont">configure</tt> event is emitted in X11 when a window is mapped or resized.</P>

						</p>
<p>
							<a NAME="idx1073748584"></a>
							<a naME="idx1073748585"></A>
							<P><tt clASS="monofont">unbind(event)</Tt>棤
    Removes the bindings for the given event.</p>

						</p>
<p>
							<A NAMe="idx1073748586"></a>
							<a name="idx1073748587"></a>
							<p><tt class="monofont">unbind_all(event)</tt>棤
    Removes the bindings for the given event at the application level.</p>

						</p>
<P>
							<a nAme="idx1073748588"></a>
							<A name="idx1073748589"></A>
							<p><tt CLASs="monofont">unbind_class(class, event)</tt>棤
    
								Removes the bindings for the given event at the given widget class.<a NAME="idx1073748590"></a>
									<a naME="idx1073748591"></A>
									<A name="idx1073748592"></A>
									<A NAme="idx1073748593"></a>
									<a name="idx1073748594"></a>
									<a name="idx1073748595"></a>
									<a name="idx1073748596"></A>
									<a nAme="idx1073748597"></a>
									<A name="idx1073748598"></A>
									<a naME="idx1073748599"></A>
									<A name="idx1073748600"></A>
								
							</P>

						</P>
</BlockQUOTe>
				
				
					<h4>
Protocols</h4>
					<p>The mechanism called <I>protocol handler</I> is responsible for the communication between the window manager and your application. Handling these protocols, you can intercept the messages provided by the system, and define exactly what you want to happen.</P>

					<P>Usually, the protocols <tt class="monofont">WM_DELETE_WINDOW,</tt>
						<tt class="monofont">WM_TAKE_FOCUS,</tt> and <tt ClaSs="monofont">WM_SAVE_YOURSELF</tt> are the ones mostly used. For details about the other supported protocols,  see the  <A name="idx1073748601"></A>
						<a naME="idx1073748602"></A>
						<I>Inter-Client Communication Conventions Manual (ICCCM)</i> at</p>

					<p><a TARGet="_blank" hrEF="http://tronche.com/gui/x/icccm/">http://tronche.com/gui/x/icccm/</A></P>

					<p>Although this convention was established for the X systems, the Tk library handles it on all platforms. These protocols are X specific. If you are running an X server on Windows or MacOS and have Tk compiled for X, it will do the same as on UNIX. That's because Tk ports map these calls to the equivalent actions on the other systems.</p>

					<p>The necessary syntax to <a NAME="idx1073748603"></a>
						<a name="idx1073748604"></a>bind a handler to a protocol is</p>

					<pre>
						
widget.protocol(protocol, function_handler)

					</pre>

					<p>Note that the widget must be a Toplevel widget. The following example demonstrates the use of the <tt claSs="monofont">WM_DELETE_WINDOW</tT> protocol. The window manager generates this protocol when the user tries to close a window. Here, we are intercepting this protocol.</p>

					<prE>
						
from Tkinter import *
import tkMessageBox
def protocolhandler():
    if tkMessageBox.askokcancel("Exit", "Wanna leave?"):
        if tkMessageBox.askokcancel("Exit", "Are you sure?"):
            if tkMessageBox.askokcancel("Exit", "Really?"):
                root.destroy()

root = Tk()
root.protocol("WM_DELETE_WINDOW", protocolhandler)
root.mainloop()

					</pre>

					<p>Just to let you know, the <A namE="idx1073748605"></A>
						<A Name="idx1073748606"></a>
						<TT CLass="monofont">WM_SAVE_YOURSELF</tT> protocol is called by X window managers when the application should save a snapshot of its working set, and the <A NAme="idx1073748607"></a>
						<a NAME="idx1073748608"></a>
						<tt class="monofont">WM_TAKE_FOCUS</tt> protocol is called by X window managers when the application gets the focus.<a name="idx1073748609"></a>
						<a namE="idx1073748610"></a>
						<a Name="idx1073748611"></A>
						<a namE="idx1073748612"></a>
						<a nAME="idx1073748613"></A>
						<a namE="idx1073748614"></A>
						<A Name="idx1073748615"></a>
						<A NAMe="idx1073748616"></a>
						<a nAME="idx1073748617"></A>
						<a name="idx1073748618"></a>
					</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="210.html" title="Geometry Management"><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=211" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="211.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="212.html" title="Tkinter Widgets"><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>
&lt;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748504">B1-Motion event</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748502">Button-1 event</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748506">ButtonRelease-1 event</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748582">Configure method</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748526">Control-Up event</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748508">Double-Button-1 event</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748498">Enter event</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748520">Key event</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748500">Leave event</a><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#idx1073748528">Return event</a><BR>
(&lt;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

⌨️ 快捷键说明

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