Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_bind_basics.py
- from Tkinter import *
- def showPosEvent(event):
- print 'X=%s Y=%s' % (event.x, event.y)
- def onKeyPress(event):
- print 'Got key press:', event.char
- def upArrowKey(event):
- print 'Got up arrow key press'
- def downArrowKey(event):
- print 'Got down arrow key press'
- def leftArrowKey(event):
- print 'Got left arrow key press'
- def rightArrowKey(event):
- print 'Got right arrow key press'
- def onReturnKey(event):
- print 'Got return key press'
- def onLeave(event):
- print 'Mouse pointer left the widget'
- def onEnter(event):
- print 'Mouse pointer entered the widget'
- def onLeftClick(event):
- print 'Got left mouse button click:',
- showPosEvent(event)
- def onRightClick(event):
- print 'Got right mouse button click:',
- showPosEvent(event)
- def onMiddleClick(event):
- print 'Got middle mouse button click:',
- showPosEvent(event)
- def onLeftDrag(event):
- print 'Got left mouse button drag:',
- showPosEvent(event)
- def onDoubleLeftClick(event):
- print 'Got double left mouse click',
- showPosEvent(event)
- root.quit()
- root = Tk()
- labelfont = ('courier', 20, 'bold') # family, size, style
- widget = Label(root, text='Hello Bind World!')
- widget.config(bg='red', font=labelfont) # red background, large font
- widget.config(height=5, width=20) # initial size: lines,chars
- widget.pack(expand=YES, fill=BOTH)
- widget.bind('<Button-1>', onLeftClick) # mouse button clicks
- widget.bind('<Button-3>', onRightClick)
- widget.bind('<Button-2>', onMiddleClick) # middle=both on some mice
- widget.bind('<Double-1>', onDoubleLeftClick) # click left twice
- widget.bind('<B1-Motion>', onLeftDrag) # click left and move
- widget.bind('<KeyPress>', onKeyPress) # all keyboard presses
- widget.bind('<Up>', upArrowKey) # arrow button pressed
- widget.bind('<Down>', downArrowKey) # arrow button pressed
- widget.bind('<Left>', leftArrowKey) # arrow button pressed
- widget.bind('<Right>', rightArrowKey) # arrow button pressed
- widget.bind('<Leave>', onLeave) # arrow button pressed
- widget.bind('<Enter>', onEnter) # return/enter key pressed
- widget.bind('<Return>', onReturnKey) # return/enter key pressed
- widget.focus() # or bind keypress to root
- root.title('Click Me')
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement