1

I am writing a program to quickly detect a pixel on the screen repeatedly and click certain buttons. As I need it to be quick, I have referenced the solution in Most efficient/quickest way to parse pixel data with Python?. The following is my code:

from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
import win32ui

'''
This is the place where those don(), ka(), bar(), balloon() functions locate. 
They are used to press certain keys automatically
'''

wd = win32ui.FindWindow(None, "太鼓ウェブ - Taiko Web")
while keyboard.is_pressed('q') == False:
    dc = wd.GetWindowDC()

    if dc.GetPixel(509, 426) == (243, 71, 40):
        don()
    if dc.GetPixel(509, 426) == (101, 189, 187):
        ka()
    if dc.GetPixel(509, 426) == (243, 181, 0):
        bar()
    if pyautogui.locateOnScreen('balloon.png', region=(485,362,290,120), grayscale=True, confidence=0.8) != None:
        balloon()

    dc.DeleteDC()

I got an error message which said: "error: GetPixel failed". I have looked up the document of win32ui.FindWindow and I know that it needs a string as a WindowName for the 2nd parameter. But how do I get the WindowName?

My target is a tab of the browser(I am using edge). I have tried using the title in the head tag in the HTML file(which is an English title), and it gives me the "error: GetPixel failed" message. And when the title contains any Japanese character like my above code, it pops out an I/O warning saying "Non-ASCII found, yet no encoding declared. Add a line like # -*- coding: cp950 -*- to your file" when I save the file. And when I run the program, it said "error: No window can be found.". I have tried adding that line at the top of my code, but it only solves the I/O warning. The "error: No window can be found." problem is still there. Also, I have tried r"太鼓ウェブ - Taiko Web" to make it raw text, but it makes no difference.

I have also tried to put None as the WindowName to get the most top-level window. And this also got me to the "error: GetPixel failed".

I have thought that the problem might be the GetWindowDC or the GetPixcel function, So I looked up the win32ui document. However, there are no such methods in the document. Where do they come from? And is this the right way to use GetPixel to compare with the RGB values?

The win32ui Document I looked up: http://timgolden.me.uk/pywin32-docs/win32ui.html

The website of "太鼓ウェブ - Taiko Web": https://taiko.bui.pm/

So long story short: how do I work with the WindowName and how do I fix the GetPixel error?

ps: I am using python2.7

Chun
  • 11
  • 2

0 Answers0