Advertisement
here2share

# Tk_mouse_plus_key.py

Aug 8th, 2022
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. # Tk_mouse_plus_key.py
  2.  
  3. from tkinter import *
  4.  
  5. root = Tk()
  6.  
  7. class Cv(): 0
  8. cv = Cv()
  9. cv.x = cv.y = 0
  10. cv.ctrl = 0
  11.  
  12. def draw():
  13.     canvas.create_oval(cv.x-5, cv.y-5, cv.x+5, cv.y+5, fill='black')
  14.  
  15. def release(e):
  16.     key = e.keysym
  17.     print("RELEASED:", key)
  18.     if key in ('Control_L'):
  19.         canvas.unbind('<B1-Motion>')
  20.  
  21. def onLeftClick(e):
  22.     cv.x = e.x
  23.     cv.y = e.y
  24.     draw()
  25.  
  26. def onLeftDrag(e):
  27.     cv.x = e.x
  28.     cv.y = e.y
  29.     if 1: # cv.ctrl:
  30.         draw()
  31.  
  32. def onCTRLdown(e):
  33.     key = e.keysym
  34.     print("PRESSED:", key)
  35.     canvas.bind('<B1-Motion>', onLeftDrag)
  36.  
  37. canvas = Canvas(root, width=400, height=400, bg='white')
  38. canvas.pack()
  39.  
  40. # needed to detect all the keyboard events <<<<<
  41. canvas.focus_set()
  42.  
  43. canvas.bind('<Button-1>', onLeftClick)
  44.  
  45. canvas.bind("<KeyRelease>", release)
  46. canvas.bind("<KeyPress>", onCTRLdown)
  47.  
  48. mainloop()
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement