Disable button/ change color based on tag values

I am trying to make a button that will start a machine, and when the machine is running/while the data is being collected I want the button to be disabled, and to change colors. I have tried to add a change script to the backgroundColor property that looks like this:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	prgrm = system.tag.readBlocking(['[default]Colder Products Company/Roseville/BurstTester/ProgramNum/Current_Program_Number'])[0].value
	child2 = system.tag.readBlocking(['[default]Colder Products Company/Roseville/BurstTester/Results/Last_Result_Child_2_UID'])[0].value
	lastID = system.tag.readBlocking(['[default]Colder Products Company/Roseville/BurstTester/Results/Last_Result_Unique_ID'])[0].value
	desiredID =  system.tag.readBlocking(['[default]Colder Products Company/Roseville/BurstTester/Results/UID_of_Desired_Results'])[0].value
	
	if prgrm == 3 and desiredID == child2 or desiredID == lastID:
		self.props.style.backgroundColor == '#37B117'
	else:
		self.props.style.backgroundColor == '#000000'

Does it look like I am on the right track, or am I way off the trail? Any ideas how to make this work?

Don't make multiple successive calls with readBlocking. The function takes an array/list of tag paths primarily to avoid these types of usages.

tag_paths = [
    	'[default]Colder Products Company/Roseville/BurstTester/ProgramNum/Current_Program_Number'
    	'[default]Colder Products Company/Roseville/BurstTester/Results/Last_Result_Child_2_UID'
    	'[default]Colder Products Company/Roseville/BurstTester/Results/Last_Result_Unique_ID'
    	'[default]Colder Products Company/Roseville/BurstTester/Results/UID_of_Desired_Results'
]
qualified_values = system.tag.readBlocking(tag_paths])
prgrm = qualified_values[0].value
child2 = qualified_values[1].value
lastID = qualified_values[2].value
desiredID = qualified_values[3].value
if prgrm == 3 and desiredID == child2 or desiredID == lastID:
	self.props.style.backgroundColor == '#37B117'
else:
	self.props.style.backgroundColor == '#000000'

But... even this isn't a very good approach. This script only executes when a singular value changes. If you're trying to determine a state or appearance based on multiple factors, you need it to update when any of the factors changes value.

Use an Expression Structure binding (and transform) in these scenarios.

Note that I'm not modifying the color here - only the enabled state. Use theme files or the stylesheet.css resource to handle styling.

/* all buttons */
button.ia_button--primary--disabled,
button.ia_button--secondary--disabled {
	background-color: #000000;
}
/* only buttons with a Perspective Style class ("MyDisabledClass") attached */
button.ia_button--primary--disabled.psc-MyDisabledClass,
button.ia_button--secondary--disabled.psc-MyDisabledClass {
	background-color: #000000;
}
/* only a button with a meta.domId value of MyButton */
#MyButton button.ia_button--primary--disabled,
#MyButton button.ia_button--secondary--disabled {
	background-color: #000000;
}
3 Likes

What Cody said, but write the logic in a single expression returning a Boolean or a style class name or whatever. That way you avoid the script + transform overhead.

1 Like