New in PyXLL 5.9
See ActiveX Controls for more information about ActiveX controls in PyXLL.
create_activex_control
(control, name=None, sheet=None, width=None, height=None, top=None, left=None, timer_interval=0.1, bridge_cls=None)Creates an ActiveX control a Python UI control object.
The control object can be any of the following:
Parameters: |
|
---|---|
Returns: |
AtxBridgeBase
Base class of bridges between the Python UI toolkits and PyXLL’s ActiveX control.
This can be used to add support for UI toolkits other than the standard ones supported by PyXLL.
__init__
(self, control)Construct the ActiveX bridge. The control is the object
passed to create_activex_control
.
pre_attach
(self, hwnd)Called before the window is attached to the ActiveX control host window.
Optional: Can be overridden in subclass
post_attach
(self, hwnd)Called after the window is attached to the ActiveX control host window.
Optional: Can be overridden in subclass
on_close
(self)Called when the ActiveX control host window is closed.
Optional: Can be overridden in subclass
on_window_closed
(self)Called when control window received a WM_CLOSE Windows message.
Optional: Can be overridden in subclass
on_window_destroyed
(self)Called when control window received a WM_DESTROY Windows message.
Optional: Can be overridden in subclass
filter_message
(self, hwnd, msg, wparam, lparam)Called when the ActiveX control host window received a Windows message.
Should return True
if the message should be filtered and not passed
to the window’s message handler, or False
otherwise.
Parameters: |
|
---|
Optional: Can be overridden in subclass
process_message
(self, hwnd, msg, wparam, lparam)Called when the ActiveX control host window received a Windows message.
Should return True
if the message was handled and shouldn’t be processed
further, or False
otherwise.
Parameters: |
|
---|
Optional: Can be overridden in subclass
translate_accelerator
(self, hwnd, msg, wparam, lparam, modifier)Called when the ActiveX control host control’s TranslateAccelerator Windows method is called.
This can be used to convert key presses into commands or events to pass to the UI toolkit control.
Should return True
if the message was handled and shouldn’t be processed
further, or False
otherwise.
Returning None is equivalent to returning (0, False).
Parameters: |
|
---|
Optional: Can be overridden in subclass
on_timer
(self)If this method is overridden then it will be called periodically and can be used to poll the UI toolkit’s message loop.
The interval between calls can be set by passing timer_interval
to create_activex_control
or by setting activex_timer_interval
in the PYXLL
section of the pyxll.cfg
config file.
Optional: Can be overridden in subclass
Sometimes you will want to create an ActiveX control when a worksheet function is called, instead of when a ribbon button or macro function is called.
For example, if you want an ActiveX control to be created each time a workbook is
opened one easy way to do that is to use a worksheet function using the recalc_on_open
argument to the xl_func
decorator. That way, whenever the workbook is opened
the function will be called and can create the control.
However, in order to create the ActiveX control when the function is called you need
to also use schedule_call
. You can’t make changes to the worksheet while
Excel is calculating and so instead you must use schedule_call
to schedule
another function to be called after Excel has finished calculating.
Additionally, you should pass name
to create_activex_control
so that
when the control is created, it re-uses any existing control with the same name rather
than creating a new control every time it is called.
For example:
from pyxll import xl_func, schedule_call
@xl_func(recalc_on_open=True)
def create_my_control():
# This inner function will be called after Excel has finished calculating
def inner_func():
# Create your widget using whichever supported UI toolkit you prefer
widget = your_code_to_create_the_widget()
# Using the same name each time means that only one control will be used,
# even if this function is called multiple times.
create_activex_control(widget, name="My Custom Control")
# Calling our inner_func function once Excel has finished calculating
schedule_call(inner_func)
See Recalculating On Open for more details about how the recalc_on_open
option works.