Python - Button Event | MUSE
In this video, you’ll learn how to create and handle basic button events using Python on an AMX MUSE controller. We’ll walk through setting up a watch on a touch panel button, defining a callback function, and inspecting the event object generated during user interaction. You’ll also see how to distinguish between button press and release events using event values. By the end, you’ll know how to trigger logic only when a button is pressed, making your control workflows more precise and efficient.
What You’ll Learn:
• Setting up button event watches in MUSE Python
• Creating and using callback functions
• Understanding the event object and its attributes
• Distinguishing between button press and release events
• Triggering logic based on specific event values
• Building more precise and efficient control workflows
Hello, and welcome to this video on creating a basic button event in your Python code. For this example, we are using code that already has a touch panel defined as dvTP. We will be using a button on port number 1 with a channel code of 1.
When you look at the descriptor file of any touch panel, all buttons are parameters. Because of this, all button events are triggered using watches[RJ1.1][MW1.2]. To create the watch, we will reference the device object, the requisite attributes, and then the watch method.
Syntactically this looks like:
*** Enter Code ***
dvTP.port[1].button[1].watch()
******************
So far, this line is only partially correct, the watch method must be passed a callback function. In this example, we will create a callback function named buttonEvent above the watch method. Type in the buttonEvent name into the watch method.
*** Enter Code ***
dvTP.port[1].button[1].watch(buttonEvent)
******************
Now define your callback function.
*** Enter Code ***
def buttonEvent(event):
*****************
The event object, created when the watch is triggered, is passed to the callback function[RJ2.1][MW2.2].
The first thing we should do is see what information is obtained when a button is pressed. Let’s create an info log that prints out the dictionary for the event. To do this, type in: context.log.info(f”Dictionary print: {event.__dict__})
*** Enter Code ***
context.log.info(f”Dictionary print: {event.__dict__})
*****************
Save your code and upload it to the controller. Once it has finished, attach to the program, and press your button. Notice when you press the button, you get two separate messages in your output bar. Let’s look at all the information we received.
The first piece of information is the source object of the event. The second is the id, in this case, the id is the channel number of the button. Next is the path of the button that was pressed, notice it matches the port and button number of the button you pressed. After that we have the device that triggered the watch event, in this case dvTP.
The next few pieces of information we receive are the reason we have two separate events. When it comes to button presses, pushing a button is one event, and releasing the button is a different event. These events produce Boolean values of true for a push and false for a release.
Now that we know what triggers a watch event on a button, and the values produced by these events, we need to write the body of our callback function to utilize that information. Usually, we would only want to trigger our button event off of either the push or the release, in this example we will use the push.
Creating a conditional that only triggers on the True value turns this button event into a “push” event. Using if(event.value == True):
*** Enter Code ***
if(event.value == True):
******************
Conversely, if your conditional were set to False, it would only trigger on the release of the button.
From there, we can enter the rest of our code. For this example, we are just going to write a simple info log of “button pushed”.
*** Enter Code ***
Context.log.info(“Button Pushed”)
******************
Save your code and upload it to the controller. Once it is uploaded, press your button. Notice that now you only receive one event per button press, and it is only triggered when you push the button.