Similar to Custom Task Panes, Python UI controls can also be embedded as a control as part of the Excel worksheet.
These can be used for custom user interfaces that form part of the worksheet. For example, controls for a dashboard or for running more complex tasks that don’t naturally fit into Excel’s grid.
Note
For user interface controls that are hosted in a panel that can be floating or docked alongside the Excel worksheet see Custom Task Panes.
PyXLL has support for the following Python UI tookits.
ActiveX controls can be created using a control or widget from any of the supported Python UI toolkits by
calling the PyXLL function create_activex_control
. ActiveX controls are objects embedded in
the Excel worksheet. The initial position and size can be set when calling create_activex_control
.
For specific details of creating an ActiveX control with any of the supported Python UI toolkits see the links above.
To move or resize the ActiveX control you first need to enable Design Mode
.
To enable Design Mode, go to the Developer
tab in the Excel ribbon and select Design Mode
.
Whilst in Design Mode a bitmap preview will be displayed instead of the web control. You can now move and resize this shape. There may be some lag between resizing the preview image and the preview image updating.
To return to the interactive web widget, unselect Design Mode.
See the API reference for create_activex_control
for more details.
The Python control hosted in an ActiveX control is not saved with the workbook.
When opening a workbook that was saved with ActiveX controls added, by default you will see a placeholder instead of Python control.
To recreate the Python control when the workbook is opened, one solution is to use
a worksheet function that recalculates when the workbook is opened. By passing name
to create_activex_control
you can update an existing control with the newly
created Python control.
See Creating an ActiveX Control from a Worksheet Function for more details about how to do this.
ActiveX controls don’t zoom in or out when zooming in or out of the Excel worksheet.
The size of the control will change as you zoom, but the size of the contents will not automatically be scaled.
You will need to bear this in mind when designing your user interface to ensure that it handles being resized correctly, if you want the Excel user to be able to change the zoom in Excel and still be able to use the control.