Where to use the script in label

perspective
I have created a new script to calculate the remaining time on duty, which runs normally. Then, I want to display the remaining time calculated by the script on the Label. However, I don't know where to call the script I wrote in the properties of the Label. Please give me some advice


Please post code, not screengrabs of code and especially not photographs of screens showing code. It means that we can copy and paste them to test or to edit in our answers. Post a screengrab as well if it helps give context to the code.

There isn't a direct script binding so the easiest way to do what you want is to use an expression binding to any nonsense value and then do a script transform.
return TimeRemaining.calculate_remaining_time()

Setup would be like this:

(My function is just returning system.data.now(). Yours will be returning the formatted time remaining.)

1 Like

wow
thank you very much,it worked

Instead of a null expression + script transform, you could use runScript('TimeRemaining.calculate_remaning_time').
I'm not a fan of runScript, but to call a library function it does the job just fine.

1 Like

class Shift:
def init(self, start_hour, end_hour):
self.start_hour = start_hour
self.end_hour = end_hour

shifts = [Shift(8, 20), Shift(20, 8)]
current_shift = 0 # 初始班次为第一个班次(早班)

def calculate_remaining_time():
global current_shift
now = datetime.datetime.now()
start_time = datetime.datetime(now.year, now.month, now.day, shifts[current_shift].start_hour, 0)
end_time = datetime.datetime(now.year, now.month, now.day, shifts[current_shift].end_hour, 0)
if now > end_time:
# 切换到下一个班次
current_shift = (current_shift + 1) % len(shifts)
start_time = datetime.datetime(now.year, now.month, now.day, shifts[current_shift].start_hour, 0)
remaining_minutes = (end_time - now).total_seconds() // 60
return remaining_minutes

print(calculate_remaining_time())Preformatted text

But there is another question that I need to consult with you. Sometimes, he doesn't update these calculated values himself. After I switch screens, he updates them himself

where should i use the runScript('TimeRemaining.calculate_remaning_time')

Your code is not formatted. There are no indents. Use the preview window to check your post before posting it.

Use now() with a millisecond value to retrigger the expression. As shown above the expression will recalculate every 5000 ms.

thanks very much

Or... use runScript. It takes a poll rate parameter to specify how often it should execute.
If you don't know where to use it, you should watch the inductive university videos and read the doc.
But I'll tell you anyway: It's an expression function, so... you can put it anywhere you'd put an expression.
in this case, you'd put it where @Transistor put the now(5000).

So, you could replace now(5000) and the transform with just runScript("TimeRemaining.calculate_remaning_time", 5000)

4 Likes

Concur. Substantially less overhead than a script transform. See the timing comments in my Integration Toolkit topic.

2 Likes

Just chiming in to say it's generally bad practice to use datetime you are much better off exclusively system.date.*functions and the java date objects it returns.

4 Likes

Since you only have two 12-hour shifts starting at 08:00 and 20:00 you could use an expression binding.

12 * 60						// 12 hours in minutes
- (
	(
		toMillis(now(1000)) // time now
		- 8 * 3600 * 1000	// 08:00 offset
	) / 1000 / 60			// convert to minutes since midnight
) % (12 * 60)				// modulo 12 hours in minutes gives minutes into shift
// Subtracting the minutes into shift from the length of the 
// shift gives the minutes remaining.

This could be more efficient than running the script.

1 Like