Advertisement
here2share

# tk_xy_outside_gui.py

Mar 27th, 2023
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. # tk_xy_outside_gui.py
  2.  
  3. import tkinter as tk
  4. import ctypes
  5. import time
  6.  
  7. ww = 300
  8. hh = 300
  9.  
  10. cx = 0
  11. cy = 0
  12.  
  13. root = tk.Tk()
  14. canvas = tk.Canvas(root, width=ww, height=hh, bg='darkgreen')
  15. canvas.pack()
  16.  
  17. # Define ctypes structures and functions
  18. class POINT(ctypes.Structure):
  19.     _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
  20.  
  21. def get_xy():
  22.     point = POINT()
  23.     ctypes.windll.user32.GetCursorPos(ctypes.byref(point))
  24.     return point.x, point.y
  25.  
  26. def gui_update(event):
  27.     global cx, cy, cw, ch
  28.     # get the root position and height
  29.     cx = root.winfo_x()
  30.     cy = root.winfo_y()
  31.     cw = root.winfo_width()
  32.     ch = root.winfo_height()
  33. gui_update(0)
  34.  
  35. root.bind("<Configure>", gui_update)
  36.  
  37. out = 50
  38. while 1:
  39.     x, y = get_xy()
  40.     x = (x - cx)
  41.     y = (y - cy)
  42.    
  43.     if -out > x or x > cw + out or -out > y or y > ch + out + 20:
  44.         canvas.config(bg='darkred')
  45.     else:
  46.         canvas.config(bg='darkgreen')
  47.    
  48.     canvas.update()
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement