Events

You can receive events from multifunction buttons (L1, L2, L3, R1, R2, R3) on the hand panels when using the local client on the microscope PC. Each button can be assigned with a custom Python function that will be executed upon pressing. We provide the ButtonHandler() class that takes care of assigning events.

Note

Don’t forget to clear the custom button assignment at the end using clear() method. This will restore the previous assignment.

See example below:

import comtypes.client as cc
from pytemscript.modules import ButtonHandler

buttons = microscope.user_buttons
buttons.show()

def my_function(x, y):
    print(x+y)

event_handler = ButtonHandler(buttons.L1, lambda: my_function(2, 3), "MyFuncName")
event_handler.assign()
cc.PumpEvents(10) # wait 10s for events (blocking)
# Now press L1, it should print the result: 5
event_handler.clear()
class pytemscript.modules.ButtonHandler(button, func: Callable, assignment: str = 'MyFunc')[source]

Create event handler for a specific hand panel button.

Parameters:
  • button (COM object) – button object

  • func (Callable) – lambda function to execute when button is pressed

  • assignment (str) – new label for the button, to be displayed in TEM User Interface

assign() None[source]

Assigns the event sink to the button.

clear() None[source]

Removes the event sink from the button.