1

我创建了简单的脚本,尝试通过在此处输入链接描述来计算权益:

import time
import win32api
import win32con

from pywinauto import application

def getEquity(ps_pid, hand1, hand2):
    def set_hand(handle, hand, kf=0):
        win32api.SendMessage(handle, win32con.WM_SETFOCUS, 0, 0) # f: losefocus
        #win32api.SendMessage(handle, win32con.WM_GETDLGCODE, 0, 0)
        time.sleep(0.05)
        len = win32api.SendMessage(handle, win32con.WM_GETTEXTLENGTH, 0, 0)
        time.sleep(0.05)
        win32api.SendMessage(handle, win32con.EM_SETSEL, 0, len)
        time.sleep(0.05)
        for c in hand:
            win32api.PostMessage(handle, win32con.WM_CHAR, ord(c), 0)
            #win32api.SendMessage(handle, win32con.WM_GETDLGCODE, 0, 0)
            time.sleep(0.05)
        win32api.SendMessage(handle, win32con.WM_KILLFOCUS, 0, 0)

    app = application.Application()
    app.connect_(process=ps_pid)

    set_hand(app.PokerStove.REdit1.handle, hand1)
    set_hand(app.PokerStove.REdit2.handle, hand2)

    app.PokerStove.Evaluate.Click()
    while app.PokerStove.EvaluateButton.WindowText() != 'Evaluate':
        time.sleep(0.1)

    return app.PokerStove.Edit12.GetLine(0)

import sys
print getEquity(int(sys.argv[1]), sys.argv[2], sys.argv[3])

我决定使用窗口消息而不是 SendKey,因为我需要让它在 PokerStove 最小化时也能正常工作。

当 PokerStove 最小化时,此脚本可以正常工作。但奇怪的事情发生的时候不是。脚本正确填充文本编辑并单击按钮,我得到了正确的结果。但在那之后将他的标题改为奇怪的东西:

扑克炉

所以看起来 PokerStove 仍在计算,但结果已经准备好。由于此更改,当我再次启动脚本时它将失败。但是当 PokerStove 最小化时,我没有这个问题。

我怀疑我在向编辑框发送消息时做错了。因为如果我手动填写它们并单击按钮,那么一切都很好。当我使用set_hand函数填充它时,即使我手动单击按钮,我也会得到这个奇怪的结果。

那么我的脚本有什么问题?

编辑:

当我将 spy++ 连接到 EvaluateButton 时,我可以看到该按钮仍然收到 WM_SETTEXT 消息,将其设置为“停止(99% 完成)”。

编辑2:

它在 Windows 7 上进行了测试。但是在家里的 VirtualBox 代码中的 Windows XP 上运行良好......

4

1 回答 1

1

你用 . 发送字符PostMessage。这个函数是异步的。嗯,关注 SendMessage 的结果可能很奇怪。

于 2012-09-06T17:39:15.527 回答