Python3.5,Win32gui,Tkinter。我无法从屏幕上获得Pixel

时间:2018-06-20 20:17:14

标签: python python-3.x tkinter win32gui

我正在尝试使用Python 3.5制作Macro。

我制作了一个程序,该程序从屏幕上搜索定义的颜色,然后单击它。

当我仅在控制台环境中对其进行测试时,它可以工作。 但是当我添加Tkinter GUI时,它不起作用。

错误消息是

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\snowscale\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
  File "D:/PyCharm/macro/main.py", line 39, in macro
    color = win32gui.GetPixel(dc, x, y)
pywintypes.error: (0, 'GetPixel', 'No error message is available')

我该如何解决?我只是猜测DeviceContext有问题,例如Tkinter根影响了它。

我不喜欢Python和GUI。所以,如果您能帮助我,我会很高兴。

这是我的代码。

import win32api
import win32gui
import win32con
import time
from tkinter import *


Area_Set = { "center_square" : (684, 471, 978, 706), "vertical_rect" : (703, 420, 1022, 838), "horizontal_rect" : (546, 524, 1152, 713), "default" : (747, 485, 1029, 814) }

selectedColor = (13421568, 13172889, 14494037)
avoidColor = (14803425, 16777215, 6908265, 16645629, 14540253, 16711422, 15592941)

clickPos = (1335, 954)
confirmPos = (1128,431)
confirmColor = 16024898
interval_arr = [35, 25, 15, 6]
x_interval = 35


def macro():
    global Area_Set
    global selectedColor
    global avoidColor
    searchArea = Area_Set["center_square"]
    global clickPos
    global confirmPos
    global confirmColor
    global interval_arr
    global x_interval

    print("started")
    while True:
        dc = win32gui.GetDC(win32gui.GetActiveWindow())
        reset = False
        for interval in interval_arr:
            for y in range(searchArea[1], searchArea[3], 8):
                endflag = False
                for x in range(searchArea[0], searchArea[2], interval):
                    color = win32gui.GetPixel(dc, x, y)
                    if color in selectedColor:
                        win32api.SetCursorPos((x, y))
                        time.sleep(0.01)
                        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                        time.sleep(0.01)
                        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                        time.sleep(0.01)
                        win32api.SetCursorPos(clickPos)
                        time.sleep(0.01)
                        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                        time.sleep(0.01)
                        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                        if win32gui.GetPixel(DC, confirmPos[0], confirmPos[1]) == confirmColor:
                            reset = True
                            win32api.SetCursorPos(confirmPos)
                            time.sleep(0.01)
                            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                            time.sleep(0.01)
                            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                        endflag = True
                        break
                if endflag:
                    break
            if endflag:
                break
        if (reset):
            continue
        else:
            break
        win32gui.ReleaseDC(dc)


root = Tk()
btn = Button(root, text="start", command=macro)
btn.pack()
root.wm_attributes("-topmost", 1)
root.mainloop()

谢谢。

1 个答案:

答案 0 :(得分:0)

已解决。当我刚改变

dc = win32gui.GetDC(win32gui.GetActiveWindow())

dc = win32gui.getDC(0)

有效。