Using the Mouse Wheel with Tkinter (Python)

  1. # explore the mouse wheel with the Tkinter GUI toolkit
  2. # Windows and Linux generate different events
  3. # tested with Python3.0
  4.  
  5. import tkinter as tk
  6.  
  7. def mouse_wheel(event):
  8. global count
  9. # respond to Linux or Windows wheel event
  10. if event.num == 5 or event.delta == -120:
  11. count -= 1
  12. if event.num == 4 or event.delta == 120:
  13. count += 1
  14. label['text'] = count
  15.  
  16. count = 0
  17. root = tk.Tk()
  18. root.title('turn mouse wheel')
  19. root['bg'] = 'darkgreen'
  20.  
  21. # with Windows OS
  22. root.bind("", mouse_wheel)
  23. # with Linux OS
  24. root.bind("", mouse_wheel)
  25. root.bind("", mouse_wheel)
  26.  
  27. label = tk.Label(root, font=('courier', 18, 'bold'), width=10)
  28. label.pack(padx=40, pady=40)
  29.  
  30. root.mainloop()

没有评论:

发表评论