import win32com.client as cc
class AgentEvents(object):
def OnClick(self, cid, button, shift, x, y):
peedy.Speak('He he he')
def OnCommand(self, UserInput):
cmdname = cc.Dispatch(UserInput).Name
print(cmdname)
if cmdname == 'hwru':
peedy.Speak('Well. Thank you.')
elif cmdname == 'yoname':
peedy.Speak('My name is Peedy.')
ag = cc.DispatchWithEvents('Agent.Control.2', AgentEvents)
ag.Connected = True
ag.Characters.Load('peedy', 'peedy.acs')
peedy = ag.Characters('peedy')
peedy.Show()
peedy.LanguageID=0x0409
#print(peedy.SRModeID)
peedy.Commands.Add('hwru','how are you','how are you',True,True)
peedy.Commands.Add('yoname',"what's your name","what's your name",True,True)
ag.CommandsWindow.Visible=True
当我从pythonwin IDE运行时,一切正常,但是从Windows命令行OnCommand事件不起作用,它没有打印错误,COM对象方法(如peedy.Speak(“Hello”))也正常工作。 是否可以将win32com activex对象与命令行中的事件一起使用?
解决方案:
while True:
# Pump messages so that COM gets a look in
pythoncom.PumpWaitingMessages()