By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

python win32com dispatch.withevents

P: 1
1.Declare, the vbcom was Global variable
2.After, vbcom was closed(use vb close button) but, the vbcom process is activating , Certainly!

3.Then, declare the vbcom was local variable but, a function(py2vb button) is not operating...

4.ASK!!!

•When the vbcom is local variable
•How do I write the code to operate the py2vb button
[ActiveX EXE]

VB6.0 Form Code
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Event Onvb2py(ByVal val As Variant)
  4. Public Event Onpy2vb(ByVal val As Variant)
  5. Public Event OnClose(ByVal idx As Integer)
  6.  
  7. Private Sub cmdClose_Click()
  8.       Unload Me
  9.       RaiseEvent OnClose(0)
  10. End Sub
  11.  
  12. Private Sub cmdPY2VB_Click()
  13.       Text1.Text = pyvalue
  14.       RaiseEvent Onpy2vb(pyvalue)
  15. End Sub
  16.  
  17. Private Sub cmdVB2PY_Click()
  18.       RaiseEvent Onvb2py(Text1.Text)
  19. End Sub
  20. --------------------------------------------------------------------------------
  21.  
  22. VB6.0 Class Code
  23.  
  24. Option Explicit
  25.  
  26. Private WithEvents fm As Form1
  27.  
  28. Event Onvb2py(ByVal val As Variant)
  29. Event Onpy2vb(ByVal val As Variant)
  30. Event OnClose(ByVal idx As Integer)
  31.  
  32. Private mpy As Variant
  33.  
  34. Public Sub Show()
  35.       fm.Show
  36. End Sub
  37.  
  38. Public Sub SetPyValue(ByVal val As Variant)
  39.       mpy = val
  40. End Sub
  41.  
  42. Private Sub Class_Initialize()
  43.       Set fm = New Form1
  44. End Sub
  45.  
  46. Private Sub Class_Terminate()
  47.       If Not fm Is Nothing Then
  48.             Set fm = Nothing
  49.       End If
  50. End Sub
  51.  
  52. Private Sub fm_OnClose(ByVal idx As Integer)
  53.       Unload fm
  54.       RaiseEvent OnClose(idx)
  55. End Sub
  56.  
  57. Private Sub fm_Onpy2vb(ByVal val As Variant)
  58.  
  59.       RaiseEvent Onpy2vb(val)
  60.       fm.Text1 = mpy
  61. End Sub
  62.  
  63. Private Sub fm_Onvb2py(ByVal val As Variant)
  64.       RaiseEvent Onvb2py(val)
  65. End Sub
  66. --------------------------------------------------------------------------------
  67.  
  68. Call Python Code
  69. import win32com.client
  70.  
  71. vbcom = win32com.client.Dispatch("nVBCOM.clsConn")
  72. vbevent = win32com.client.WithEvents(vbcom,VBEventHandler)
  73.  
  74. class VBEventHandler:
  75.     def Onpy2vb(self,val):
  76.         val = "python...value"
  77.         vbcom.SetPyValue(val)
  78.  
  79.     def Onvb2py(self,val):
  80.         print "vbvalue 2 python : " + val
  81.  
  82.     def OnClose(self,val):
  83.         print "Close"
  84.  
  85. def run():
  86.     try:
  87.         vbcom.SetPyValue("Intial py value")
  88.         vbcom.Show()
  89.     except:
  90.         print "Error"
  91.  
  92. if __name__ == "__main__":
  93.     run()
Jan 16 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.