Advertisement
here2share

# Tk_bind_basics.py

Sep 29th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. # Tk_bind_basics.py
  2.  
  3. from Tkinter import *
  4.  
  5. def showPosEvent(event):
  6.     print 'X=%s Y=%s' % (event.x, event.y)
  7.  
  8. def onKeyPress(event):
  9.     print 'Got key press:', event.char
  10.  
  11. def upArrowKey(event):
  12.     print 'Got up arrow key press'
  13.  
  14. def downArrowKey(event):
  15.     print 'Got down arrow key press'
  16.  
  17. def leftArrowKey(event):
  18.     print 'Got left arrow key press'
  19.  
  20. def rightArrowKey(event):
  21.     print 'Got right arrow key press'
  22.  
  23. def onReturnKey(event):
  24.     print 'Got return key press'
  25.  
  26. def onLeave(event):
  27.     print 'Mouse pointer left the widget'
  28.  
  29. def onEnter(event):
  30.     print 'Mouse pointer entered the widget'
  31.  
  32. def onLeftClick(event):
  33.     print 'Got left mouse button click:',
  34.     showPosEvent(event)
  35.  
  36. def onRightClick(event):
  37.     print 'Got right mouse button click:',
  38.     showPosEvent(event)
  39.  
  40. def onMiddleClick(event):
  41.     print 'Got middle mouse button click:',
  42.     showPosEvent(event)
  43.  
  44. def onLeftDrag(event):
  45.     print 'Got left mouse button drag:',
  46.     showPosEvent(event)
  47.  
  48. def onDoubleLeftClick(event):
  49.     print 'Got double left mouse click',
  50.     showPosEvent(event)
  51.     root.quit()
  52.  
  53. root = Tk()
  54. labelfont = ('courier', 20, 'bold')                 # family, size, style
  55. widget = Label(root, text='Hello Bind World!')
  56. widget.config(bg='red', font=labelfont)             # red background, large font
  57. widget.config(height=5, width=20)                   # initial size: lines,chars
  58. widget.pack(expand=YES, fill=BOTH)
  59.  
  60. widget.bind('<Button-1>',  onLeftClick)             # mouse button clicks
  61. widget.bind('<Button-3>',  onRightClick)
  62. widget.bind('<Button-2>',  onMiddleClick)           # middle=both on some mice
  63. widget.bind('<Double-1>',  onDoubleLeftClick)       # click left twice
  64. widget.bind('<B1-Motion>', onLeftDrag)              # click left and move
  65.  
  66. widget.bind('<KeyPress>',  onKeyPress)              # all keyboard presses
  67. widget.bind('<Up>',        upArrowKey)              # arrow button pressed
  68. widget.bind('<Down>',      downArrowKey)            # arrow button pressed
  69. widget.bind('<Left>',      leftArrowKey)            # arrow button pressed
  70. widget.bind('<Right>',     rightArrowKey)           # arrow button pressed
  71. widget.bind('<Leave>',     onLeave)                 # arrow button pressed
  72. widget.bind('<Enter>',     onEnter)                 # return/enter key pressed
  73. widget.bind('<Return>',    onReturnKey)             # return/enter key pressed
  74. widget.focus()                                      # or bind keypress to root
  75. root.title('Click Me')
  76. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement