Re: How to correctly handle events using win32com ?
David Sansom <[email protected]> Sat, 30 Nov 2024 10:04:52 +0000
| Newsgroups | gmane.comp.python.windows |
|---|---|
| Message-ID | <[email protected]> |
--===============6580277926567973255== Content-Type: multipart/alternative; boundary=Apple-Mail-F8A0E2B3-EC17-4286-97E5-BC9C41501EC5 Content-Transfer-Encoding: 7bit --Apple-Mail-F8A0E2B3-EC17-4286-97E5-BC9C41501EC5 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi, > I guess I should send a WM_QUIT message but I don't know how. Have you tried win32api.PostQuitMessage() in the event handler for Code 1? DS Sent from my iPhone > On 29 Nov 2024, at 18:00, Gauthier ABRIAL <[email protected]> wrote: >=20 > =EF=BB=BF > Hello, > I'm looking for some advice on how to correctly handle events using win32c= om. If I take the example of a very simple Excel automation, I tested two th= ings. > Code1 below use pythoncom.PumpMessages() but I don't know how to stop it w= hen the Excel is closing. I guess I should send a WM_QUIT message but I don'= t know how. > Code2 below use a while loop on pythoncom.PumpWaitingMessages() but once I= stop the loop the COM server freeze as it is waiting for the messages it se= nds to be processed before closing. I guess I should pump all the remaining m= essages but I don't know how. > Maybe I should use a totally different approach. > Thanks a lot for your help. >=20 > G. >=20 > -------------------- Code1 Starts ----------------------- > import win32com.client as win32 > import pythoncom >=20 > #The event handlers > class wbEvents: > def OnBeforeClose(self, Cancel): > print('Closing Workbook') > # Send WM_QUIT here ? >=20 > xlApp =3D win32.Dispatch("Excel.Application") #Open Excel > xlApp.Visible=3DTrue #Make Excel Visible > xlwb =3D xlApp.Workbooks.Add() #Create a workbook > ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet > xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler > pythoncom.PumpMessages() > -------------------- Code1 Ends ----------------------- >=20 > -------------------- Code2 Starts ----------------------- > import win32com.client as win32 > import pythoncom > import time >=20 > #The event handlers > class wbEvents: > def OnBeforeClose(self, Cancel): > print('Closing Workbook') > global keepOpen > keepOpen =3D False >=20 > xlApp =3D win32.Dispatch("Excel.Application") #Open Excel > xlApp.Visible=3DTrue #Make Excel Visible > xlwb =3D xlApp.Workbooks.Add() #Create a workbook > ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet > xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler > # define initalizer > keepOpen =3D True > while keepOpen: > time.sleep(0.1) > pythoncom.PumpWaitingMessages() > -------------------- Code2 Ends ----------------------- >=20 > _______________________________________________ > python-win32 mailing list > [email protected] > https://mail.python.org/mailman/listinfo/python-win32 --Apple-Mail-F8A0E2B3-EC17-4286-97E5-BC9C41501EC5 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable <html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=3D= utf-8"></head><body dir=3D"auto">Hi,<div><br></div><div><blockquote type=3D"= cite"><div dir=3D"ltr"><ul data-editing-info=3D"{"applyListStyleFromLev= el":false,"unorderedStyleType":2}"><li style=3D"font-family: C= alibri, Helvetica, sans-serif; font-size: 12pt; list-style-type: "- &qu= ot;;"><div class=3D"elementToProof"> I guess I should send a WM_QUIT me= ssage but I don't know how.</div></li></ul></div></blockquote><div><br></div= ><div>Have you tried win32api.PostQuitMessage() in the event handler for Cod= e 1?</div><div><br></div><div>DS</div><div><br id=3D"lineBreakAtBeginningOfS= ignature"><div dir=3D"ltr">Sent from my iPhone</div><div dir=3D"ltr"><br><bl= ockquote type=3D"cite">On 29 Nov 2024, at 18:00, Gauthier ABRIAL <g_abria= [email protected]> wrote:<br><br></blockquote></div><blockquote type=3D"cite"= ><div dir=3D"ltr">=EF=BB=BF <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-1= "> <div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, sans= -serif; font-size: 12pt; color: rgb(0, 0, 0);"> Hello,</div> <div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, sans= -serif; font-size: 12pt; color: rgb(0, 0, 0);"> I'm looking for some advice on how to correctly handle events using win32com= . If I take the example of a very simple Excel automation, I tested two thin= gs.</div> <ul data-editing-info=3D"{"applyListStyleFromLevel":false,"un= orderedStyleType":2}"> <li style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0); list-style-type: "- ";"> <div class=3D"elementToProof">Code1 below use pythoncom.PumpMessages() but I= don't know how to stop it when the Excel is closing. I guess I should send a= WM_QUIT message but I don't know how.</div> </li><li style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12= pt; color: rgb(0, 0, 0); list-style-type: "- ";"> <div class=3D"elementToProof" style=3D"margin: 0px;">Code2 below use a while= loop on pythoncom.PumpWaitingMessages() but once I stop the loop the COM se= rver freeze as it is waiting for the messages it sends to be processed befor= e closing. I guess I should pump all the remaining messages but I don't know how.</div> </li></ul> <div class=3D"elementToProof" style=3D"margin: 0px; font-family: Calibri, He= lvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"> Maybe I should use a totally different approach.</div> <div class=3D"elementToProof" style=3D"margin: 0px; font-family: Calibri, He= lvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"> Thanks a lot for your help.</div> <div class=3D"elementToProof" style=3D"margin: 0px; font-family: Calibri, He= lvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"> <br> </div> <div class=3D"elementToProof" style=3D"margin: 0px; font-family: Calibri, He= lvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"> G.</div> <div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, sans= -serif; font-size: 12pt; color: rgb(0, 0, 0);"> <br> </div> <div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, sans= -serif; font-size: 12pt; color: rgb(0, 0, 0);"> -------------------- Code1 Starts -----------------------</div> <div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, sans= -serif; font-size: 12pt; color: rgb(0, 0, 0);"> import win32com.client as win32</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> import pythoncom</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> <br> </div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> #The event handlers</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> class wbEvents:</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> def OnBeforeClose(self, Cancel):</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> print('Closing Workbook')</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> # Send WM_QUIT here ?</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> <br> </div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> xlApp =3D win32.Dispatch("Excel.Application") #Open Excel</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> xlApp.Visible=3DTrue #Make Excel Visible</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> xlwb =3D xlApp.Workbooks.Add() #Create a workbook</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> pythoncom.PumpMessages()</div> <div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, sans= -serif; font-size: 12pt; color: rgb(0, 0, 0);"> -------------------- Code1 Ends -----------------------</div> <div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, sans= -serif; font-size: 12pt; color: rgb(0, 0, 0);"> <br> </div> <div class=3D"elementToProof" style=3D"text-align: left; text-indent: 0px; m= argin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; co= lor: rgb(0, 0, 0);"> -------------------- Code2 Starts -----------------------</div> <div style=3D"text-align: left; text-indent: 0px; margin: 0px; font-family: C= alibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"> import win32com.client as win32</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> import pythoncom</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> import time</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> <br> </div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> #The event handlers</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> class wbEvents:</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> def OnBeforeClose(self, Cancel):</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> print('Closing Workbook')</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> global keepOpen</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> keepOpen =3D False</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> <br> </div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> xlApp =3D win32.Dispatch("Excel.Application") #Open Excel</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> xlApp.Visible=3DTrue #Make Excel Visible</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> xlwb =3D xlApp.Workbooks.Add() #Create a workbook</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> # define initalizer</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> keepOpen =3D True</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> while keepOpen:</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> time.sleep(0.1)</div> <div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> pythoncom.PumpWaitingMessages()</div> <div class=3D"elementToProof" style=3D"text-align: left; text-indent: 0px; m= argin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; co= lor: rgb(0, 0, 0);"> -------------------- Code2 Ends -----------------------</div> <div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, sans= -serif; font-size: 12pt; color: rgb(0, 0, 0);"> <br> </div> <span>_______________________________________________</span><br><span>python= -win32 mailing list</span><br><span>[email protected]</span><br><span>= https://mail.python.org/mailman/listinfo/python-win32</span><br></div></bloc= kquote></div></div></body></html>= --Apple-Mail-F8A0E2B3-EC17-4286-97E5-BC9C41501EC5-- --===============6580277926567973255== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ python-win32 mailing list [email protected] https://mail.python.org/mailman/listinfo/python-win32 --===============6580277926567973255==--