首页 > Python > Tkinter中的Events事件(关于bind()的详细描述)
2016
08-17

Tkinter中的Events事件(关于bind()的详细描述)

 

关于Tkinter中Events事件,看过不少教程,基本都是简单说明event参数的简单使用,基本就是event.x和event.y(返回鼠标的x,y坐标),其他就再无详细说明。

        遍寻网络,终于找到这一篇文章,详细说明了event的各种参数,感谢作者。

        另外,《Python与Tkinter编程》的附录部分也有相关的描述,可惜我的pdf版本质量太差,看得太累。

******************************************************************************************************************************************

    事件响应是GUI无论如何都绕不过的一道坎。要实现类似QQ的文本框读取键盘输入,按Enter发送数据的便捷操作,单靠Focus显然不行了,这次是非要使用Event,整个软件的架构又要改,想哭~~~Python:Tkinter-EventsPython:Tkinter-Events
***************************************************************************************************
    An event is something that happens to your application--for example, the user presses a key to clicks ot drag the mouse--to which the application needs to react.
基本概念:
(1)An event is some occurrence that your application needs to know about.
(2)An event handler is a function in your application that gets call when an event occurs.
(3)We call it binding when your application sets up an event handler that gets called when an event happens to a widget.
****************************************************************************************************
Binding:绑定事件和事件处理函数
    有三个层次的binding:Instance binding, Class binding, Application binding(这个吓尿了)
(1)Instance binding:将事件响应绑定到指定的widget,使用widget.bind(sequence, func, add);
(2)Class binding:将事件响应绑定到一个类型下的全部widgets,使用bind_class(class, sequence, func, add);
(3)Application binding:当事件发生时,无论Application的哪个widget在focus状态下,都会产生handler响应,使用bind_all(sequence, func, add);
****************************************************************************************************
Event sequences:包含一个或多个事件模板的字符串(a string containing one or more event patterns)
事件模板:<[modifier-]...type[-detail]>
(1)事件模板放在<>中;
(2)type表示事件的类型,例如key press 或 mouse click;
(3)modifier可以增加一些组合操作,例如shift 或 control;
(4)detail描述具体的key,对于鼠标而言指代的哪个键,1 for button 1, 2 for button 2, 3 for button 3
下面是一个例子:
<Button-1>:鼠标的第一个键
<KeyPress-H>:键盘的H键
<Control-Shift-KeyPress-H>:键盘的control-shift-H键
****************************************************************************************************
Event type:事件类型(有点多啊Python:Tkinter-EventsPython:Tkinter-EventsPython:Tkinter-Events

  1. Activate:widget从非激活状态到激活状态

  2. Button:点击鼠标按键(The user pressed one of the mouse buttons)

  3. ButtonRelease:松开鼠标按键(The user let up on a mouse button)

  4. Configure:用户改变widget的尺寸

  5. Deactivate:widget由激活状态变成非激活状态

  6. Destroy:widget被销毁

  7. Enter:用户将鼠标移动到widget上

  8. Expose:当被其它窗口覆盖后再出现时触发(This event occus whenever at least some part of your application or widget becomes visible after having been coverd up by another window)

  9. FocusIn:widget获得输入的focus

  10. FocusOut:input focus从widget中移出

  11. KeyPress:用户点击键盘上的按键(The user pressed a key on the keyboaed)

  12. KeyRelease:用户松开键盘上的按键(The user let up a key on the keyboard)

  13. Leave:用户将鼠标移除widget

  14. Map:widget变得可见(A widget is being mapped, that is, made visible in the application)

  15. Motion:用户将整个鼠标移入widget(The user moved the mouse pointer entirely within a widget)

  16. MouseWheel:用户滑动鼠标滚轮

  17. Unmap:widget变得不可见(A widget is being unmapped and is no longer visible)

  18. Visibility:当application的某部分在屏幕上变的可见(Happens when at least some part of the application windoww becomes visible on the screen)

 

****************************************************************************************************

Event modifiers:事件修饰语

  1. Alt:alt按键

  2. Any:任意的按键

  3. Control:control按键

  4. Double:短时间内事件发生两次(Specifies two events happending close together in time)

  5. Lock:shift lock按键

  6. Shift:shift按键

  7. Triple:Triple kill,和double一样的原理

***************************************************************************************************

Key names:键盘上按键的名字(这个真心太多了)

    键盘对应的Event types是keyPress和KeyRelease,有三种不同的方法来表示:

  1. keysym:key的字符串名字

  2. keycode:key code,表示哪个键被按下

  3. keysym_num:shows a numeric code equivalent to the key symbol

***************************************************************************************************

Writing your hangler:The Event class(重点来了Python:Tkinter-EventsPython:Tkinter-Events

    The sections above tell you how to describe what events you want to handle, and how to bind them. Now let us turn to the writing of the handler that will be called when the event actually happens.

handler:传递一个Event object,通知事件的产生。它可以是一个函数,也可以是一个类型的方法:

def hanglerName(event):

def handlerName(self, event):

下面是Event object的属性:

  1. char:KeyPress和KeyRelease Event,char被设为该character

  2. delta:MouseWheel Event

  3. height:Configure Event,表示widget的新的height(像素)

  4. keycode:KeyPress和KeyRelease Event, 表示数字按键的值

  5. keysym:KeyPress和KeyRelease Event,表示一些特殊的按键的值

  6. keysym_num:KeyPress和KeyRelease Event,this is set to a numeric version of the .keysym field

  7. num:鼠标按键,1,2,3

  8. serial:An integer serail number that is incremented every time the server processes a client request

  9. state:A integer describing the state of all the modifier keys

  10. time:两次时间发生的时间间隔

  11. type:A numeric code describing the type of event

  12. widget:指向触发事件的widget

  13. width:Configure Event,表示widget的新的width(像素)

  14. x:当事件发生时,鼠标所在处的Y坐标,以widget的左上角为原点

  15. y:当事件发生时,鼠标所在处的Y坐标,以widget的左上角为原点

  16. x_root:当事件发生时,鼠标所在处的X坐标,以屏幕的左上角为原点

  17. y_root:当事件发生时,鼠标所在处的Y坐标,以屏幕的左上角为原点

***************************************************************************************************

下面是各种例子:

(1)绑定到点击鼠标左键,这样在Frame中点击鼠标的左键会显示位置信息


结果如下:

click at 20 17
click at 28 30
click at 37 46
click at 77 59
click at 73 44
===========================================================================

(2)绑定键盘消息

键盘输入事件只发送给获得focus的widget,可以使用focus_set()函数


结果如下:

pressed 'a'
pressed 's'
pressed 'f'
pressed 's'
pressed 'f'
=================================================================================

(3)最后是我真正要解决的问题,敲击Enter发送,其它的所有输入都是写入文本框

我X,突然发现这个功能的实现和Enter键一点关系没有,绑定文本框和事件Enter就可以了,只不过Enter可以完成相同的功能罢了



已瞎

 

作者:admin
admin
TTF的家园-www.ttfde.top 个人博客以便写写东西,欢迎喜欢互联网的朋友一起交流!

本文》有 0 条评论

留下一个回复