Mouse click doesn't work

import pytesseract
from PIL import ImageGrab, ImageEnhance, ImageOps
import pyautogui
import time
import openai
import tkinter as tk
from threading import Thread
import random
import re

Define API keys

api_keys = [

]

Path to Tesseract installation

pytesseract.pytesseract.tesseract_cmd = r’C:\Program Files\Tesseract-OCR\tesseract.exe’

Capture screen image and read text

def capture_screen_and_read_text():
screen_image = ImageGrab.grab() # Capture the entire screen
gray_image = ImageOps.grayscale(screen_image) # Convert to grayscale
enhanced_image = ImageEnhance.Contrast(gray_image).enhance(2) # Increase contrast
text = pytesseract.image_to_string(enhanced_image, lang=‘tur’) # Perform OCR with Turkish language support
return text

Check if the text is a question

def is_question(text):
return bool(re.search(r’?$', text.strip()))

Get response from AI

def get_ai_response(question):
while True:
key = random.choice(api_keys) # Choose a random API key
openai.api_key = key
try:
response = openai.Completion.create(
engine=“gpt-3.5-turbo”,
prompt=question,
max_tokens=50
)
answer = response.choices[0].text.strip()
return answer
except openai.error.RateLimitError:
print(“Rate limit exceeded, waiting for 60 seconds…”)
time.sleep(60)
except openai.error.InvalidRequestError as e:
print(f"Invalid request: {e}“)
break
except Exception as e:
print(f"An error occurred: {e}”)
break

Find the answer and click on it

def find_and_click_answer(text):
answer = get_ai_response(text)
if not answer:
return

screen_image = ImageGrab.grab()
click_found = False

for i in range(0, screen_image.width, 10):
    for j in range(0, screen_image.height, 10):
        region = screen_image.crop((i, j, i + 100, j + 30))
        region_text = pytesseract.image_to_string(region, lang='tur')
        
        if answer.lower() in region_text.lower():
            x = i + 50
            y = j + 15
            canvas.create_rectangle(x-5, y-5, x+5, y+5, outline="red", fill="red")
            canvas.update()
            
            print(f"Attempting to click at coordinates: ({x}, {y})")

            pyautogui.moveTo(x, y)  # Move mouse to coordinates
            pyautogui.click()  # Perform click action
            status_label.config(text=f"Click successful: ({x}, {y})", fg="green")
            click_found = True
            break
    if click_found:
        break

if not click_found:
    status_label.config(text="Answer not found or unable to click", fg="red")

Check if the bot is running

bot_running = False

Start the bot

def start_bot():
global bot_running
bot_running = True
bot_thread = Thread(target=automate_question_answering)
bot_thread.start()
status_label.config(text=“Bot is running”, fg=“green”)

Stop the bot

def stop_bot():
global bot_running
bot_running = False
status_label.config(text=“Bot stopped”, fg=“red”)

Automated question-answering process

def automate_question_answering():
global bot_running
while bot_running:
text = capture_screen_and_read_text()
print(f"Text on the screen: {text}")

    if is_question(text):
        print(f"Detected question: {text}")
        find_and_click_answer(text)

    # Wait for a short time between operations
    time.sleep(3)

Create GUI

root = tk.Tk()
root.title(“Bot Control Panel”)

Create and add Canvas to the window

canvas = tk.Canvas(root, width=800, height=600, bg=“white”)
canvas.pack(padx=10, pady=10)

Create Start and Stop buttons

start_button = tk.Button(root, text=“Start Bot”, command=start_bot)
start_button.pack(pady=10)

stop_button = tk.Button(root, text=“Stop Bot”, command=stop_bot)
stop_button.pack(pady=10)

Status label

status_label = tk.Label(root, text=“Bot stopped”, fg=“red”)
status_label.pack(pady=10)

Run the window

root.mainloop()

Blockquote

Please put all of the code between 1 pair of triple backtick lines, not 2 small parts between 2 pairs of such. Then drastically reduce the code to the MINIMUM needed to show your problem.