我的SendMessage/PostMessage部分的代码有什么问题?

2024-09-30 14:23:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个简单的脚本,它试图获得由enter link description here计算的权益:

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

所以看起来扑克炉还在计算,但结果已经准备好了。因为这个改变,当我再次启动脚本时,它将失败。但当扑克炉子最小化时,我就没有这个问题了。在

我怀疑我把信息发送到编辑框是做错了什么。因为如果我手动填充,然后点击按钮,一切都好。当我使用set_hand函数填充它时,即使我手动单击按钮,也会得到这个奇怪的结果。在

我的剧本怎么了?在

编辑:

当我把spy++连接到EvaluateButton时,我可以看到这个按钮仍然得到WM嫘setext消息,它将它设置为“Stop(99%完成)”。在

编辑2:

它在Windows7上进行了测试。但是在家里,在WindowsXP上用VirtualBox编写的代码运行得很好。。。在


Tags: import脚本apptimesyssleephandle扑克