3 This demo shows how to embed an ActiveX control in a wxPython
4 application, (Win32 only.)
7 The MakeActiveXClass function dynamically builds a new Class on the
8 fly, that has the same signature and semantics as wxWindow. This
9 means that when you call the function you get back a new class that
10 you can use just like wxWindow, (set the size and position, use in a
11 sizer, etc.) except its contents will be the COM control.
14 This demo embeds the Internet Exploer WebBrowser control, and shows
15 how to receive events from the COM control. (The title bar and status
16 bar are updated as pages change, in addition to the log messages being
21 from wxPython
.wx
import *
23 if wxPlatform
== '__WXMSW__':
24 from wxPython
.lib
.activexwrapper
import MakeActiveXClass
25 import win32com
.client
.gencache
28 browserModule
= win32com
.client
.gencache
.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
30 raise ImportError("IE4 or greater does not appear to be installed.")
33 #----------------------------------------------------------------------
35 class TestPanel(wxWindow
):
36 def __init__(self
, parent
, log
, frame
=None):
37 wxWindow
.__init
__(self
, parent
, -1)#, style=wxCLIP_CHILDREN)
40 self
.current
= "http://wxPython.org/"
43 self
.titleBase
= frame
.GetTitle()
46 sizer
= wxBoxSizer(wxVERTICAL
)
47 btnSizer
= wxBoxSizer(wxHORIZONTAL
)
49 # Make a new class that derives from the WebBrowser class in the
50 # COM module imported above. This class also derives from wxWindow and
51 # implements the machinery needed to integrate the two worlds.
52 theClass
= MakeActiveXClass(browserModule
.WebBrowser
,
55 # Create an instance of that class
56 self
.ie
= theClass(self
, -1, style
=wxSUNKEN_BORDER
)
59 #btn = wxButton(self, wxNewId(), " Open ")
60 #EVT_BUTTON(self, btn.GetId(), self.OnOpenButton)
61 #btnSizer.Add(btn, 0, wxEXPAND|wxALL, 5)
63 btn
= wxButton(self
, wxNewId(), " Home ")
64 EVT_BUTTON(self
, btn
.GetId(), self
.OnHomeButton
)
65 btnSizer
.Add(btn
, 0, wxEXPAND|wxALL
, 5)
67 btn
= wxButton(self
, wxNewId(), " <-- ")
68 EVT_BUTTON(self
, btn
.GetId(), self
.OnPrevPageButton
)
69 btnSizer
.Add(btn
, 0, wxEXPAND|wxALL
, 5)
71 btn
= wxButton(self
, wxNewId(), " --> ")
72 EVT_BUTTON(self
, btn
.GetId(), self
.OnNextPageButton
)
73 btnSizer
.Add(btn
, 0, wxEXPAND|wxALL
, 5)
75 txt
= wxStaticText(self
, -1, "Location:")
76 btnSizer
.Add(txt
, 0, wxCENTER|wxALL
, 5)
78 self
.location
= wxComboBox(self
, wxNewId(), "", style
=wxCB_DROPDOWN
)
79 EVT_COMBOBOX(self
, self
.location
.GetId(), self
.OnLocationSelect
)
80 EVT_KEY_UP(self
.location
, self
.OnLocationKey
)
81 #EVT_CHAR(self.location, self.IgnoreReturn)
82 btnSizer
.Add(self
.location
, 1, wxEXPAND|wxALL
, 5)
84 sizer
.Add(btnSizer
, 0, wxEXPAND
)
85 sizer
.Add(self
.ie
, 1, wxEXPAND
)
87 self
.ie
.Navigate(self
.current
)
88 self
.location
.Append(self
.current
)
91 self
.SetAutoLayout(true
)
92 EVT_SIZE(self
, self
.OnSize
)
95 def OnSize(self
, evt
):
103 def OnLocationSelect(self
, evt
):
104 url
= self
.location
.GetStringSelection()
105 self
.log
.write('OnLocationSelect: %s\n' % url
)
106 self
.ie
.Navigate(url
)
108 def OnLocationKey(self
, evt
):
109 if evt
.KeyCode() == WXK_RETURN
:
110 URL
= self
.location
.GetValue()
111 self
.location
.Append(URL
)
112 self
.ie
.Navigate(URL
)
116 def IgnoreReturn(self
, evt
):
118 if evt
.KeyCode() != WXK_RETURN
:
121 def OnOpenButton(self
, event
):
122 dlg
= wxTextEntryDialog(self
, "Open Location",
123 "Enter a full URL or local path",
124 self
.current
, wxOK|wxCANCEL
)
126 if dlg
.ShowModal() == wxID_OK
:
127 self
.current
= dlg
.GetValue()
128 self
.ie
.Navigate(self
.current
)
131 def OnHomeButton(self
, event
):
132 self
.ie
.GoHome() ## ET Phone Home!
134 def OnPrevPageButton(self
, event
):
138 def OnNextPageButton(self
, event
):
142 # The following event handlers are called by the web browser COM
143 # control since we passed self to MakeActiveXClass. It will look
144 # here for matching attributes and call them if they exist. See the
145 # module generated by makepy for details of method names, etc.
146 def OnBeforeNavigate2(self
, pDisp
, URL
, *args
):
147 self
.log
.write('OnBeforeNavigate2: %s\n' % URL
)
149 def OnNavigateComplete2(self
, pDisp
, URL
):
150 self
.log
.write('OnNavigateComplete2: %s\n' % URL
)
152 self
.location
.SetValue(URL
)
154 def OnTitleChange(self
, text
):
155 self
.log
.write('OnTitleChange: %s\n' % text
)
157 self
.frame
.SetTitle(self
.titleBase
+ ' -- ' + text
)
159 def OnStatusTextChange(self
, text
):
160 self
.log
.write('OnStatusTextChange: %s\n' % text
)
162 self
.frame
.SetStatusText(text
)
165 #----------------------------------------------------------------------
166 # for the demo framework...
168 def runTest(frame
, nb
, log
):
169 if wxPlatform
== '__WXMSW__':
170 win
= TestPanel(nb
, log
, frame
)
173 dlg
= wxMessageDialog(frame
, 'This demo only works on MSW.',
174 'Sorry', wxOK | wxICON_INFORMATION
)
181 #----------------------------------------------------------------------
184 if __name__
== '__main__':
185 class TestFrame(wxFrame
):
187 wxFrame
.__init
__(self
, None, -1, "ActiveX test -- Internet Explorer",
189 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
190 self
.CreateStatusBar()
191 self
.tp
= TestPanel(self
, sys
.stdout
, self
)
192 EVT_CLOSE(self
, self
.OnCloseWindow
)
194 def OnCloseWindow(self
, event
):
199 app
= wxPySimpleApp()