Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14185

pyautogui.pixel() doesn't update when looping

$
0
0

I'm trying to make a simple script to help me automate a task in a game, and the trigger I'm looking to use is when a specific pixel on screen becomes a specific color. I'm using pyautogui.pixwl(), but when I run my function in a while loop, it returns the same value, even if the pixel is changed. It only seems to update when I alt+tab out and back in. It only seems to happen with the game when it is an active window though, seems to work fine elsewise.

Code in question:

import pyautoguiimport timedef get_pixel_color(x, y):"""    Get the RGB value of a specific pixel on the screen.    Parameters:    - x (int): X-coordinate of the pixel.    - y (int): Y-coordinate of the pixel.    Returns:    - tuple: RGB value as a tuple (R, G, B)."""    return pyautogui.pixel(x, y)# Example usage:x_coordinate = 1090  # Replace with the actual X-coordinatey_coordinate = 92  # Replace with the actual Y-coordinatewhile True:    pixel_color = get_pixel_color(x_coordinate, y_coordinate)    print(f"The color at pixel ({x_coordinate}, {y_coordinate}) is {pixel_color}")    time.sleep(1)

Example output:

The color at pixel (1090, 92) is (25, 179, 25)The color at pixel (1090, 92) is (25, 179, 25)The color at pixel (1090, 92) is (25, 179, 25)The color at pixel (1090, 92) is (25, 179, 25)The color at pixel (1090, 92) is (25, 179, 25)The color at pixel (1090, 92) is (25, 179, 25)The color at pixel (1090, 92) is (160, 98, 73) <-- This is where I alt tab out and back in, seems to update the values.The color at pixel (1090, 92) is (160, 98, 73)The color at pixel (1090, 92) is (160, 98, 73)The color at pixel (1090, 92) is (160, 98, 73)The color at pixel (1090, 92) is (160, 98, 73)The color at pixel (1090, 92) is (160, 98, 73)

Any advice on how to fix this?


Viewing all articles
Browse latest Browse all 14185

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>