Events(事件)

Events are given as strings, using a special event syntax:
事件以字符串的方式给出,使用特殊的事件语法:
<modifier-type-detail>
Event Formats(事件格式)
1、<Button-1>
在小部件上按下鼠标按钮触发事件
2、<B1-Motion>
按下按钮并移动触发事件
3、<ButtonRelease-1>
松开按钮触发事件
4、<Double-Button-1>
鼠标点击两次后触发事件
5、<Enter>
鼠标进入小部件之后触发(不是点击或者摁enter键)
6、<Leave>
鼠标离开小部件时触发
7、<FocusIn>
Keyboard focus was moved to this widget, or to a child of this widget.
没明白这个什么意思
8、<FocusOut>
Keyboard focus was moved from this widget to another widget.
应该是跟上边意思相反但是不明白
9、<Key>
键盘输入任意键触发
10、<Return>
The user pressed the Enter key. You can bind to virtually all keys on the keyboard. For an ordinary 102-key PC-style keyboard, the special keys are Cancel (the Break key), BackSpace, Tab, Return(the Enter key), Shift_L (any Shift key), Control_L (any Control key), Alt_L (any Alt key), Pause, Caps_Lock, Escape, Prior (Page Up), Next (Page Down), End, Home, Left, Up, Right, Down, Print, Insert, Delete, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Num_Lock, and Scroll_Lock.(不明白什么意思)
11、a
The user typed an “a”. Most printable characters can be used as is. The exceptions are space (<space>) and less than (<less>). Note that 1 is a keyboard binding, while <1> is a button binding.
12、<Shift-Up>
The user pressed the Up arrow, while holding the Shift key pressed. You can use prefixes like Alt, Shift, and Control.
13、<Configure>
The widget changed size (or location, on some platforms). The new size is provided in the width and height attributes of the event object passed to the callback.

The Event Object

The event object is a standard Python object instance, with a number of attributes describing the event.
事件属性:
widget
The widget which generated this event. This is a valid Tkinter widget instance, not a name. This attribute is set for all events.(生成此事件的小部件。这是一个有效的Tkinterwidget属性,不是一个名称。这个属性可以为所有事件设置)

x, y
The current mouse position, in pixels.(当前鼠标位置,以像素为单位。x,y轴)

x_root, y_root
The current mouse position relative to the upper left corner of the screen, in pixels.(当前鼠标位置相对于屏幕左上角的像素)

char
The character code (keyboard events only), as a string.(字符代码(只有键盘事件),作为字符串)

keysym
The key symbol (keyboard events only).(关键符号(只有键盘事件))

keycode
The key code (keyboard events only).(关键代码(只有键盘事件))

num
The button number (mouse button events only).(按钮编号(鼠标按键事件))

width, height
The new size of the widget, in pixels (Configure events only).(小部件的新大小,以像素(仅配置事件))

type
The event type.(事件类型)

Instance and Class Bindings(实例和类绑定)
you can create bindings on four different levels:

1、the widget instance, using bind.

2、the widget’s toplevel window (Toplevel or root), also using bind.

3、the widget class, using bind_class (this is used by Tkinter to provide standard bindings).

4、the whole application, using bind_all.
首先,在这四个层次中,Tkinter选择可用绑定的 “最接近的匹配”。例如,如果为<Key>和 <Return>事件创建实例绑定,则只有按下Enter键才会调用第二个绑定。
例子1:
#-*-coding:utf-8-*-
#author:wangxing

#捕获键盘事件

import Tkinter

root = Tkinter.Tk()
def key(event):
print 'pressed',repr(event.char)
def callback(event):
frame.focus_set()
print 'clicked at',event.x_root,event.y_root, event.num
x = event.x
y = event.y
return x,y
def enter():
print 'test'
frame = Tkinter.Frame(root,width=100,height=100,bg='red')
'''
frame.bind("<Key>",key)
frame.bind("<Button-1>",callback)
frame.bind("<B1-Motion>",callback)
frame.bind("<ButtonRelease-1>",callback)
frame.bind("<Double-Button-1>",callback)
frame.bind("<Enter>",callback)
frame.bind("<Leave>",callback)
'''
frame.bind("<FocusIn>",key)
frame.bind("<Button-1>",callback)
frame.pack()
root.mainloop()
例子2:
#-*-coding:utf-8-*-
#author:wangxing

'''Capturing clicks in a window(在窗口中捕获单击的位置下,y)'''
from Tkinter import *

root = Tk()

def callback(event):
print "clicked at", event.x, event.y

frame = Frame(root, width=100, height=100,bg='black')
frame.bind("<Button-1>", callback)
frame.pack()
root.mainloop()

Protocol(协议)
除了事件绑定之外,Tkinter还支持称为协议处理程序的机制。
最常用的协议称为WM_DELETE_WINDOW,用于定义用户使用窗口管理器明确关闭窗口时发生的情况:
例子:
#-*-coding:utf-8-*-
#author:wangxing

import Tkinter,tkMessageBox

def callback():
if tkMessageBox.askyesno('退出','你确定要退出?'):
root.destroy()

root = Tkinter.Tk()
root.protocol("WM_DELETE_WINDOW",callback)
root.mainloop()

Standard Dialogs(标准对话框)
The tkMessageBox module provides an interface to the message dialogs.

The easiest way to use this module is to use one of the convenience functions: showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, or askretrycancel.

They all have the same syntax:
tkMessageBox.function(title, message [, options]).

Message Box Options(选项)
1、default constant

Which button to make default: ABORT, RETRY, IGNORE, OK, CANCEL, YES, or NO (the constants are defined in the tkMessageBox module).

2、icon (constant)

Which icon to display: ERROR, INFO, QUESTION, or WARNING

3、message (string)

The message to display (the second argument to the convenience functions). May contain newlines.

4、parent (widget)

Which window to place the message box on top of. When the message box is closed, the focus is returned to the parent window.

5、title (string)

Message box title (the first argument to the convenience functions).

6、type (constant)

Message box type; that is, which buttons to display: ABORTRETRYIGNORE, OK, OKCANCEL, RETRYCANCEL, YESNO, or YESNOCANCEL.

例子:

#-*-coding:utf-8-*-
#author:wangxing import Tkinter master = Tkinter.Tk()
w = Tkinter.Canvas(master,width=200,height=100)
w.pack() w.create_line(0,0,200,100,fill='blue')
w.create_line(0,100,200,0,fill='red',dash=(2,4)) #dash破折号
w.create_rectangle(50,25,150,75,fill='red') Tkinter.mainloop()

---恢复内容结束---

python Tkinter图形用户编程简单学习(一)的更多相关文章

  1. Python模块——loguru日志模块简单学习

    Python loguru模块简单学习 首先安装模块:pip install logoru,然后引入模块: from loguru import logger 1.直接输出到console logge ...

  2. Python实战之网络编程socket学习笔记及简单练习

    sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 参数一:地址簇 socket.AF_INET IPv4(默认) socket.AF_IN ...

  3. Python: Tkinter、ttk编程之计算器

    起源: 研究Python UI编程,我偏喜欢其原生组件,于是学习Tkinter.ttk组件用法.找一计算器开源代码,略加修整,以为备忘.其界面如图所示: 1.源代码(Python 2.7): # en ...

  4. SQL数据库—<4>SQL编程--简单学习

    数据类型:列的类型 变量定义:declare 变量名 类型名    变量名:以@开头 赋值和取值:    赋值:set/select 变量=值    取值:变量名例:declare @a varcha ...

  5. Linux——用户管理简单学习笔记(四)

    主要讲两个用户管理的案例: 1: 限制用户su为root,只允许某个组的的用户su # groupadd sugroup 首先添加我们的用户组 # chmod 4550 /bin/su 改变命令的权限 ...

  6. Linux——用户管理简单学习笔记(三)

    用户组管理命令: groupadd -g 888 webadmin 创建用户组webadmin,其GID为888 删除用户组: groupdel 组名 修改用户组信息 groupmod groupmo ...

  7. Linux——用户管理简单学习笔记(二)

    其实如果我们了解了Linux中用户管理的配置文件之后,完全可以手工管理用户: 添加用户: useradd 设置选项 用户名 -D 查看缺省参数 u:UID g:缺省所属用户组GID G:指定用户所属多 ...

  8. Linux——用户管理简单学习笔记(一)

    Linux用户分为三种: 1:超级用户(root,UID=0) 2:普通用户(UID 500-60000) 3:伪用户(UID 1-499)  伪用户: 1.伪用户与系统和程序服务相关 :nbin.d ...

  9. Python Tkinter 图形组件介绍

    1. 窗口 Tkinter.Tk() # -*- coding: UTF-8 -*- import Tkinter myWindow = Tkinter.Tk() myWindow.title('南风 ...

随机推荐

  1. <2014 05 16> 线性表、栈与队列——一个环形队列的C语言实现

    栈与队列都是具有特殊存取方式的线性表,栈属于先进后出(FILO),而队列则是先进先出(FIFO).栈能够将递归问题转化为非递归问题,这是它的一个重要特性.除了FILO.FIFO这样的最普遍存取方式外, ...

  2. 【我的Android进阶之旅】Android调用JNI出错 java.lang.UnsatisfiedLinkError: No implementation found for的解决方法

    错误描述 今天使用第三方的so库时候,调用JNI方法时出现了错误.报错如下所示: 11-01 16:39:20.979 4669-4669/com.netease.xtc.cloudmusic E/a ...

  3. Dom4j总结

    1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和 JAXP. DOM4J ...

  4. (4.8)SET ANSI_NULLS ON、SET QUOTED_IDENTIFIER ON

    T-SQL支持在与空值进行比较时,允许比较运算符返回 TRUE 或 FALSE. 通过设置 ANSI_NULLS OFF 可将此选项激活.当 ANSI_NULLS 为 OFF 时,如果 ColumnA ...

  5. Android studio 如何快速收起代码?

    windows下 ctrl+shift+(小键盘上的减号 -) mac下 commang+shift+减号,搞定

  6. Maven学习笔记—私服(包含maven的setting.xml配置)

    为什么要用远程仓库(私服) 如果没有私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的maven仓库下载到本地,而一个团队中的所有人都重复的从maven仓库下载构件,这样就加大了中央仓库 ...

  7. python学习之路-第八天-文件IO、储存器模块

    文件IO.储存器模块 文件IO 代码示例: # -*- coding:utf-8 -*- #! /usr/bin/python # filename:using_file.py poem = '''\ ...

  8. 关于shared pool的深入探讨(五)

    Oracle使用两种数据结构来进行shared pool的并发控制:lock 和 pin.Lock比pin具有更高的级别. Lock在handle上获得,在pin一个对象之前,必须首先获得该handl ...

  9. PHP连接局域网MYSQL数据库的实例

    PHP连接局域网MYSQL数据库的例子. 代码:                           ?>         第一行ip对应局域网某台主机,关于局域网内mysql连接可参考我的摘抄 ...

  10. Python(正则 re模块)

    1. 匹配一个字符 表达式 说明 等价表达式 \d 数字 [0-9] \w 字母.数字.下划线 [a-zA-Z0-9_] . 除换行外任意字符   \s 空格 [\t\n\r\f\v] \D 除数字 ...