Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_xy_outside_gui.py
- import tkinter as tk
- import ctypes
- import time
- ww = 300
- hh = 300
- cx = 0
- cy = 0
- root = tk.Tk()
- canvas = tk.Canvas(root, width=ww, height=hh, bg='darkgreen')
- canvas.pack()
- # Define ctypes structures and functions
- class POINT(ctypes.Structure):
- _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
- def get_xy():
- point = POINT()
- ctypes.windll.user32.GetCursorPos(ctypes.byref(point))
- return point.x, point.y
- def gui_update(event):
- global cx, cy, cw, ch
- # get the root position and height
- cx = root.winfo_x()
- cy = root.winfo_y()
- cw = root.winfo_width()
- ch = root.winfo_height()
- gui_update(0)
- root.bind("<Configure>", gui_update)
- out = 50
- while 1:
- x, y = get_xy()
- x = (x - cx)
- y = (y - cy)
- if -out > x or x > cw + out or -out > y or y > ch + out + 20:
- canvas.config(bg='darkred')
- else:
- canvas.config(bg='darkgreen')
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement