1

I've created a simple program that clicks black tiles in a piano.

from pyautogui import *
import pyautogui
import time
import keyboard
import random   
import win32api, win32com
import image


#RGB (1,1,1)
#TILE 1 X = 353 Y = 380
#TILE 2 X = 404 Y = 380
#TILE 3 X = 455 Y = 380
#TILE 4 X = 511 Y = 380


def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(0.01)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)

while keyboard.is_pressed("q") == False:

    if pyautogui.pixel(353, 380) [0] == 1:
        click(353,380)
    if pyautogui.pixel(404, 380) [0] == 1:
        click(404,380)
    if pyautogui.pixel(455, 380) [0] == 1:
        click(455,380)
    if pyautogui.pixel(511, 380) [0] == 1:
        click(511,380)


raise WindowsError("windll.user32.ReleaseDC failed : return 0")
OSError: windll.user32.ReleaseDC failed : return 0

It gives this error, does anyone know why? I tried to search for it but nothing showed up.

EDIT (this is the error):

Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 122, in __win32_openDC
    raise WindowsError("windll.user32.ReleaseDC failed : return 0")
OSError: windll.user32.ReleaseDC failed : return 0
Rafael
  • 13
  • 4
  • Are you raising the `WindowsError` intentionally? Or is that a part of the traceback? – gmdev May 06 '21 at 16:33
  • It's part of the traceback – Rafael May 06 '21 at 16:34
  • Could you include the full traceback so that we can see what line in your code causes this error? :) – gmdev May 06 '21 at 16:35
  • Yes, there it is! – Rafael May 06 '21 at 16:39
  • There should be more to the traceback; it should show which line of **your** code causes the error :). What version of Python are you using? – gmdev May 06 '21 at 16:41
  • I'm using python 3.8 – Rafael May 06 '21 at 16:44
  • [This](https://stackoverflow.com/a/59595934/12326283) answer suggests trying to run on Python 3.7, instead of 3.8. For example, in terminal (if Python3.8 is installed): `python3.7 your file.py` – gmdev May 06 '21 at 16:46
  • 1
    I created [an issue](https://github.com/asweigart/pyautogui/issues/579) for this on github as there are easily 5-10 posts on this issue on SO as well as numerous others on reddit. So many that it's hard to find details on `windll.user32.ReleaseDC` itself because all the hits are for pyautogui issues! I'd follow along with the github issue. In my opinion, all the "just downgrade" suggestions are not answers, nor solutions. That said, this isn't a howto question either (fitting for SO), it's a bug and needs to be addressed upstream, not here. – Hendy Jun 10 '21 at 17:14
  • @gmdev here's [the source](https://github.com/asweigart/pyscreeze/blob/0446e87235e0079f591f0c49ece7d487dedc2f9a/pyscreeze/__init__.py#L112). – Hendy Jun 10 '21 at 17:15
  • There is a pending [PR](https://github.com/asweigart/pyscreeze/pull/73) to `pyscreeze` that addresses this now. Probably a while until this gets release, but one could use my fork directly in the meantime. – Hendy Jun 15 '21 at 15:03

0 Answers0