Untitled
unknown
python
2 years ago
4.7 kB
18
Indexable
import tkinter as tk
from tkinter import ttk
import pygetwindow as gw
import pyautogui
from PIL import ImageGrab
import pytesseract
class Overlay(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.configure(background='black')
# Cover all screens
self.geometry(f"{self.winfo_screenwidth()}x{self.winfo_screenheight()}+0+0")
self.attributes('-alpha', 0.3) # Make the window transparent
self.bind('<Button-1>', self.on_click)
self.bind('<ButtonRelease-1>', self.on_release)
self.start_x = None
self.start_y = None
self.parent_app = None # Will be set by the App class
def on_click(self, event):
self.start_x = self.winfo_pointerx()
self.start_y = self.winfo_pointery()
def on_release(self, event):
end_x = self.winfo_pointerx()
end_y = self.winfo_pointery()
self.parent_app.rectangle_coords = (self.start_x, self.start_y, end_x, end_y)
self.parent_app.rect_label.config(text=f"Selected Rectangle: {self.parent_app.rectangle_coords}")
self.destroy()
class App:
def __init__(self, root):
self.root = root
self.selected_app = None
self.rectangle_coords = None
self.app_list = []
self.monitoring = False # Flag to control monitoring
self.root.title("Text Recognition and Interaction Tool")
self.create_selection_area()
self.create_info_display()
def create_selection_area(self):
selection_frame = tk.Frame(self.root)
selection_frame.pack(pady=20)
rect_btn = tk.Button(selection_frame, text="Select Rectangle", command=self.start_rectangle_selection)
rect_btn.pack(side=tk.LEFT, padx=10)
self.app_var = tk.StringVar()
self.update_app_list()
self.app_dropdown = ttk.Combobox(selection_frame, textvariable=self.app_var, values=self.app_list)
self.app_dropdown.pack(side=tk.LEFT, padx=10)
self.app_dropdown.bind("<<ComboboxSelected>>", lambda e: self.select_application())
refresh_btn = tk.Button(selection_frame, text="Refresh Apps", command=self.update_app_list)
refresh_btn.pack(side=tk.LEFT, padx=10)
start_btn = tk.Button(selection_frame, text="Start", command=self.start_monitoring)
start_btn.pack(side=tk.LEFT, padx=10)
stop_btn = tk.Button(selection_frame, text="Stop", command=self.stop_monitoring)
stop_btn.pack(side=tk.LEFT, padx=10)
def create_info_display(self):
info_frame = tk.Frame(self.root)
info_frame.pack(pady=10)
self.rect_label = tk.Label(info_frame, text="Selected Rectangle: None")
self.rect_label.pack()
self.app_label = tk.Label(info_frame, text="Selected Application: None")
self.app_label.pack()
self.status_label = tk.Label(info_frame, text="Status: Idle")
self.status_label.pack()
def start_rectangle_selection(self):
self.overlay = Overlay(self.root)
self.overlay.parent_app = self
def select_application(self):
self.selected_app = self.app_var.get()
self.app_label.config(text=f"Selected Application: {self.selected_app}")
def update_app_list(self):
self.app_list = [w.title for w in gw.getAllWindows() if w.visible and w.title]
if hasattr(self, 'app_dropdown'):
self.app_dropdown['values'] = self.app_list
if self.app_list:
self.app_var.set(self.app_list[0])
self.select_application()
def start_monitoring(self):
if self.rectangle_coords and self.selected_app and not self.monitoring:
self.monitoring = True
self.status_label.config(text="Status: Monitoring")
self.monitor_area()
def stop_monitoring(self):
self.monitoring = False
self.status_label.config(text="Status: Idle")
def monitor_area(self):
if self.monitoring and self.rectangle_coords and self.selected_app:
screenshot = ImageGrab.grab(self.rectangle_coords)
text = pytesseract.image_to_string(screenshot)
if "Kliknij" in text:
self.focus_and_press_space()
self.root.after(1000, self.monitor_area) # Schedule next check in 1 second
def focus_and_press_space(self):
app_window = gw.getWindowsWithTitle(self.selected_app)[0]
app_window.activate()
pyautogui.press('space')
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
Editor is loading...
Leave a Comment