211.html

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

HTML
484
字号

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Handling Tkinter Events</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="206.html" class="navtitle">15. Tkinter</a> &gt; <span class="nonavtitle">Handling Tkinter Events</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<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>
<a href="5%2F31%2F2002+4%3A48%3A59+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148036103062149000105224</font><a href="read1.asp?bookname=0672319942&snode=211&now=5%2F31%2F2002+4%3A48%3A59+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>





Handling Tkinter Events</h3>
				<p>Usually, when you create a graphical interface for your application, you want to handle all the possible events that happen there, such as reading in each key in the keyboard (including the F1朏12 set, Ctrl, Alt, and Shift keys), tracking the actions upon the mouse button, or even controlling the window redraw events fired by the window manager. Tkinter handles that by allowing you to create bindings for every specific object. Actually, you can <a NAME="idx1073748490"></a>
					<a naME="idx1073748491"></A>bind events to the widget instance itself, to the widget's Toplevel window, to the widget's class, and to your entire application (such as a global HELP functionality for the F1 function key).</P>

				<p>After binding an event to a widget, you need to specify which function should be called at the time the event occurs. This function (or method) is called a <a naME="idx1073748492"></A>
					<A name="idx1073748493"></a>
					<i>callback.</i> You can define callbacks for all kinds of windowing events, as you will see later. The following code demonstrates a simple callback functionality, which is associated to the <a name="idx1073748494"></a>
					<a name="idx1073748495"></a>
					<tT clAss="monofont">command</tT> property from a specific widget.</p>

				<pre>
					
from Tkinter import *
import sys
def close():
    sys.exit(0)

root = Tk()
button = Button(root)
button['text'] = "Close"
button['command'] = close
button.pack()
root.mainloop()

				</Pre>

				<p>The next example binds the mouse-click event <A NAMe="idx1073748496"></a>
					<a nAME="idx1073748497"></A>(<tt clASS="monofont">"&lt;Button-1&gt;"</Tt>) to a specific function in our program. Note that the event description is just a simple string. The mainloop keeps checking for this event, and when it catches the event, the function (event handler) is called. Note that an object is passed to the callback function carrying some information provided by the event.</p>

				<prE>
					
from Tkinter import *
def ShowPosition(event):
    Top = Toplevel(root)
    xlabel = Label(Top)
    xlabel.pack()
    xlabel.config(text = "X = " + str(event.x))
    ylabel = Label(Top)
    ylabel.pack()
    ylabel.config(text = "Y = " + str(event.y))
    Top.mainloop()

root = Tk()
frame = Frame(root, width=200, height=200)
frame.bind("&lt;Button-1&gt;", ShowPosition)
frame.pack()
root.mainloop()

				</PRE>

				<p>The next sections provided more events that you can use in your programs.</p>

				
					<h4>
Mouse Events</h4>
					<p>When handling mouse event, use 1 for the left button, 2 for the middle button, and 3 for the right button. The following events are based on the left button, and you need to make the necessary changes in order to adapt them for usage with the other buttons. Before starting, you should know that the current position of the mouse pointer, the position relative to the widget, is provided in the x and y options of the event object passed to the callback.</p>

					<p>If you bind to both a single click event and to a double click event, both bindings will be called whenever one of them is activated.</p>

					<blockquote>
<p>
							<a NamE="idx1073748498"></a>
							<a nAme="idx1073748499"></a>
							<p><Tt clASS="monofont">&lt;Enter&gt;</Tt>棤
    
								The mouse pointer entered the widget.</p>

						</p>
<p>
							<A NAMe="idx1073748500"></a>
							<a nAME="idx1073748501"></A>
							<p><tt cLASS="monofont">&lt;Leave&gt;</tt>棤
    
								The mouse pointer left the widget.</p>

						</p>
<p>
							<a name="idx1073748502"></a>
							<a name="idx1073748503"></a>
							<p><tt ClaSs="monofont">&lt;Button-1&gt;,</tt>
									<Tt claSs="monofont">&lt;ButtonPress-1&gt;,</tt> or 

<TT CLass="monofont">&lt;1&gt;</tT>棤
    
								A mouse button is pressed over the widget.</P>

						</P>
<P>
							<a namE="idx1073748504"></A>
							<A Name="idx1073748505"></a>
							<P><TT Class="monofont">&lt;B1-Motion&gt;</tt>棤
    
								The mouse is moved, with mouse button 1 being held down.</p>

						</p>
<p>
							<a name="idx1073748506"></a>
							<a name="idx1073748507"></A>
							<p><tT claSs="monofont">&lt;ButtonRelease-1&gt;</tt>棤
    
								Button 1 was released.</p>

						</P>
<p>
							<a nAME="idx1073748508"></A>
							<a namE="idx1073748509"></A>
							<P><Tt claSS="monofont">&lt;Double-Button-1&gt;</TT>棤
    
								Button 1 was double-clicked.<a namE="idx1073748510"></A>
									<A Name="idx1073748511"></a>
									<a name="idx1073748512"></a>
									<a name="idx1073748513"></a>
									<a namE="idx1073748514"></a>
									<a Name="idx1073748515"></A>
									<a namE="idx1073748516"></a>
									<a nAME="idx1073748517"></A>
									<a namE="idx1073748518"></A>
									<A Name="idx1073748519"></a>
								
							</P>

						</P>
</BLockqUOTE>
				
				
					<h4>
Keyboard Events</h4>
					<p>The following events are exposed by the keyboard interface:</p>

					<blockquote>
<p>
							<a name="idx1073748520"></A>
							<a nAme="idx1073748521"></a>
							<P><tt clAss="monofont">&lt;Key&gt;</tT>棤
    
								The user has pressed any key. The instance object originated by the callback function carries an attribute called <I>char</I> that can be used to identify which key was pressed.
							</P>

						</p>
<p>
							<a nAME="idx1073748522"></A>
							<a namE="idx1073748523"></A>
							<P><Tt claSS="monofont">a</TT>棤
    The user typed the letter a.</p>

						</p>
<p>
							<a name="idx1073748524"></a>
							<a name="idx1073748525"></a>
							<p><tt clAss="monofont">b</Tt>棤
    
								The user typed the letter b.
							</p>

						</p>
</BlockQuotE>
					<P>The same concept can be applied for all the other printable characters.</P>

					<BlockQUOTe>
<p>
							<a nAME="idx1073748526"></A>
							<a namE="idx1073748527"></A>
							<P><Tt class="monofont">&lt;Control-Up&gt;</tt>棤
    
								The user pressed the Control key, while pressing the Up arrow. This type of structure also allows you to use the keyword suffixes Up, Down, Left, and Right, and the keyword prefixes Control, Alt, and Shift.
							</p>

						</p>
<p>
							<a name="idx1073748528"></a>
							<a naMe="idx1073748529"></a>
							<P><tt cLass="monofont">&lt;Return&gt;</tT>棤
    The user pressed the Enter key.</p>

						</p>
<p>
							<A NAMe="idx1073748530"></a>
							<a nAME="idx1073748531"></A>
							<p><tt cLASS="monofont">&lt;Escape&gt;</tt>棤
    The user pressed the Esc key.
							</p>

						</p>
</BLOCkquote>
					<p>The same concept can also be applied for all the other special keys found in the keyboard, including: F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Num_Lock, Scroll_Lock, Caps_Lock, Print, Insert, Delete, Pause, Prior <i>(Page Up)</i>, Next <i>(Page Down),</i> BackSpace, Tab, Cancel <i>(Break)</i>, Control_L (any Control key), Alt_L (any Alt key), Shift_L (any Shift key), End, Home, Up, Down, Left, and Right.<a name="idx1073748532"></a>
						<a NamE="idx1073748533"></a>
						<a nAme="idx1073748534"></a>
						<a Name="idx1073748535"></A>
						<A NAme="idx1073748536"></a>
						<a NAME="idx1073748537"></a>
						<a naME="idx1073748538"></A>
						<A name="idx1073748539"></A>
						<A NAme="idx1073748540"></a>
						<a name="idx1073748541"></a>
					</p>

				
				
					<h4>
Event Attributes</h4>
					<p>Next, I list all the attributes that are exposed by the instance objects originated by the callback functions:</p>

					<blockqUotE>
<p>
							<a nAme="idx1073748542"></a>
							<a Name="idx1073748543"></A>
							<P><TT clasS="monofont">char</TT>棤
    Character code associated with a pressed key.</P>

						</p>
<p>
							<p>
								<tT CLAss="monofont">keycode</tt>棤
    Key code associated with a pressed key.</P>

						</P>
<P>
							<A name="idx1073748544"></a>
							<a name="idx1073748545"></a>

⌨️ 快捷键说明

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