Add grid.py

This commit is contained in:
admin
2026-05-25 18:34:08 +00:00
parent f45eebce62
commit 6a1ed576ff

132
grid.py Normal file
View File

@@ -0,0 +1,132 @@
# og source - thirds.exe
import tkinter as tk
import ctypes
from PIL import Image, ImageTk
from pynput import mouse, keyboard
# Windows Constants
WS_EX_LAYERED = 0x00080000
WS_EX_TRANSPARENT = 0x00000020
GWL_EXSTYLE = -20
HWND_TOPMOST = -1
SWP_NOSIZE = 0x0001
SWP_NOMOVE = 0x0002
SWP_NOACTIVATE = 0x0010
def make_window_click_through(hwnd):
styles = ctypes.windll.user32.GetWindowLongPtrW(hwnd, GWL_EXSTYLE)
ctypes.windll.user32.SetWindowLongPtrW(hwnd, GWL_EXSTYLE, styles | WS_EX_LAYERED | WS_EX_TRANSPARENT)
def make_window_always_on_top(hwnd):
ctypes.windll.user32.SetWindowPos(
hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE
)
def create_red_square_icon(size=16):
img = Image.new("RGBA", (size, size), color=(255, 0, 0, 255))
return ImageTk.PhotoImage(img)
def main():
root = tk.Tk()
root.title("Thirds")
# 1
icon_image = create_red_square_icon(size=32)
root.iconphoto(True, icon_image)
# 2
root.overrideredirect(True)
root.wm_attributes("-topmost", True)
# 3
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 4
root.geometry(f"{screen_width}x{screen_height}+0+0")
# 5
TRANSPARENT_COLOR = "magenta"
root.config(bg=TRANSPARENT_COLOR)
root.wm_attributes("-transparentcolor", TRANSPARENT_COLOR)
# Rule-of-Thirds lines
canvas = tk.Canvas(
root,
width=screen_width,
height=screen_height,
highlightthickness=0,
bg=TRANSPARENT_COLOR
)
canvas.pack()
# Rule of Thirds lines
line_v1 = canvas.create_line(screen_width/3, 0,
screen_width/3, screen_height,
fill="red", width=2)
line_v2 = canvas.create_line(2*screen_width/3, 0,
2*screen_width/3, screen_height,
fill="red", width=2)
line_h1 = canvas.create_line(0, screen_height/3,
screen_width, screen_height/3,
fill="red", width=2)
line_h2 = canvas.create_line(0, 2*screen_height/3,
screen_width, 2*screen_height/3,
fill="red", width=2)
# window to be drawn
root.update_idletasks()
# native window handle via the window title
hwnd = ctypes.windll.user32.FindWindowW(None, "Rule of Thirds Overlay")
if not hwnd:
hwnd = root.winfo_id() # fallback
# window click-through
make_window_click_through(hwnd)
# window always on top
make_window_always_on_top(hwnd)
# Listeners
grid_visible = True
def toggle_grid():
nonlocal grid_visible
grid_visible = not grid_visible
new_state = "normal" if grid_visible else "hidden"
for line_id in (line_v1, line_v2, line_h1, line_h2):
canvas.itemconfig(line_id, state=new_state)
def change_color(new_color):
for line_id in (line_v1, line_v2, line_h1, line_h2):
canvas.itemconfig(line_id, fill=new_color)
def close_program():
root.destroy()
def on_click(x, y, button, pressed):
if pressed and button == mouse.Button.middle:
toggle_grid()
mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()
hotkeys = keyboard.GlobalHotKeys({
'<shift>+1': lambda: change_color('red'), # Shift+1 → Red
'<shift>+2': lambda: change_color('light green'), # Shift+2 → Light green
'<shift>+3': lambda: change_color('white'), # Shift+3 → White
'<shift>+4': lambda: change_color('black'), # Shift+4 → Black
'<ctrl>+1': close_program # Ctrl+1 → Close
})
hotkeys.start()
root.mainloop()
if __name__ == "__main__":
main()