Skip to content

Instantly share code, notes, and snippets.

@renyuanL
Created July 29, 2014 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renyuanL/59d8ee4ddd91affb6f5e to your computer and use it in GitHub Desktop.
Save renyuanL/59d8ee4ddd91affb6f5e to your computer and use it in GitHub Desktop.
Tk的事件.py
'''
Tk的事件.py
Ref:
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
'''
from tkinter import *
窗= Tk()
布= Canvas(窗)
布.pack()
事件列表=[
'<Enter>', # 鼠入窗
'<Leave>', # 鼠離窗
'<Button-1>', # 鼠鈕1(左)單擊
'<Double-Button-1>', # 鼠鈕1(左)雙擊
'<Triple-Button-1>', # 鼠鈕1(左)三擊
'<ButtonRelease-1>', # 鼠鈕1(左)釋放
'<B1-Motion>', # 鼠鈕1(左)移動
'<FocusIn>', # 窗聚焦(鼠點擊窗內),
'<FocusOut>',# 窗失焦(鼠點擊窗外)
'<Key>', # 鍵盤按鍵
'<Configure>' # 調整窗大小
]
for 事件 in 事件列表:
def 萬用函數(e, ef= 事件):
#
# 可查看 event 中,有什麼 attribute 可用。
#
a= dir(e)
#print(a)
print(ef, end='')
'''
for x in a:
if x[0]!='_':
print(x, eval('e.'+x))
'''
if ef in ['<Button-1>', '<B1-Motion>']:
print(e.x, e.y)
x,y= e.x, e.y
布.create_oval(x,y,x+10,y+10,fill= 'red')
elif ef in ['<Key>']:
print(e.char, e.keysym, hex(e.keysym_num))
if e.keysym in ['Delete']:
布.delete('all')
else:
x,y, 字= e.x, e.y, e.char
布.create_text(x, y, text= 字)
elif ef in ['<Configure>']:
print(e.width, e.height)
布['width']= e.width -4 # -4 is necessary
布['height']= e.height -4 # 要不然,窗會被布 擠大 或 縮小!
else:
pass
窗.bind(事件, 萬用函數)
窗.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment