Toggle button Label

Hi all,

thanks in advance,

I'm facing an issue in the perspective component - toggle button
tbl

here I have script for color & Label. I used the toggle button on the flex repeater
tglflx

while saving the data in the database as default it values come as true & false.
I tried a for loop to convert it but it's not working
let's consider a parameter as status

Status = ["False","True","True","False"]
StatusCount =int(len(Status))
for v in range (StatusCount):
if Status != "False":
a = "ok"
print(a)
else:
a = "Not Okay"
print(a)

A few tips:

  1. The toggle is a switch - not a button.
  2. The toggle has a label property. There's no need for a separate label.
  3. Standard windows GUI is that the label remains the same and the switch shows the status. i.e., Every label would say "OK" and the switch show the status. Having "Not OK" gives rise to a double-negative, "The system is not Not OK". Would that mean that it is OK?
  4. Switch text would normally be aligned with the switch, not centre-aligned as you have it shown. "OK" (both letters capitalised) is normal.
  5. You are using strings. ["False", "True", "True", "False"]. You should be using booleans. [False, True, True, False]. Your code then becomes
if not Status:
  1. It appears that you are using a switch as an indicator. Why? It doesn't look right. It looks like more of an application for an indicator.

LED indicator

Here I've used 32 × 32 labels and created two styles, LEDgreen and LEDred. These have a border set, the corner radius set to 16 and the fill set to theme colors --success and --error so that they match whatever Perspective theme I'm using.
However, beware of only using color to indicate status as I have done here as color-blind operators may have a hard time distinguishing the two states.

https://docs.inductiveautomation.com/display/DOC81/Perspective+Built-In+Themes

  1. When posting code on the forum (or any other) format it using the </> button. It preserves indents (essential for Python) and applies syntax highlighting.
2 Likes
  1. format your code by using the code button
    image
    or by wrapping your code in triple backticks:

```
code
```

Keep in mind that whitespace is significant in python, and you need to use proper code markup in order to keep the indentation when pasting code here.
Now, in this case, the code is simple enough that we can deduce what it really is, but don't leave formatting to us, that's your job.

  1. you need to be more descriptive about what you're trying to do and what is not working.
    For example, you say "while saving the data in the database": What data ? How do you save it ?

  2. I suggest you read this: https://xyproblem.info/

While I have no idea what you're trying to do, I can tell you right now that the script you showed is not going to do what you expect.

Status = ["False","True","True","False"]
StatusCount =int(len(Status))
for v in range (StatusCount):
	if Status != "False":
		a = "ok"
		print(a)
	else:
		a = "Not Okay"
		print(a)
  • Status = ["False","True","True","False"]
    Why are there 4 statuses for a toggle ? Why are you using strings instead of booleans ?
  • StatusCount =int(len(Status))
    You don't need to cast to int, len() already returns one
  • if Status != "False":
    Status is your list, it can never be equal to a string
  • You're checking the same thing for each iteration of your loop. Nothing changes there.

What you probably meant to do was this:

statuses = [False, True, True, False]
for status in statuses:
	if status:
		a = "ok"
	else:
		a = "Not Okay"
	print a

But I don't think you really need a script here.
I think you took a wrong path somewhere and everything became more complicated than it needs to be. That's the xy problem described in the page I linked above.
Tell us more about what you want to do, not how you're trying to do it.

1 Like

yeah thanks for your response,

as I already mentioned I'm using a flex repeater in that template i used toggle switch
for ex: i have 4 instances as i shared the pic
so, from the instances I received the output has shown below

Status = ["False","True","True","False"]

#here I'm trying to get the length by len() func. & storing it on the variable as StatusCount.

StatusCount =int(len(Status))

#here I'm trying to get the length by len() func. & storing it on the variable as StatusCount. #the value will be 4

#here i'm passing it for the iterarion
for v in range (StatusCount):
if Status != "False":
a = "ok"
print(a)
else:
a = "Not Okay"
print(a)


want I'm trying to do means:

I need a input as ok or not okay anyone
so by using toggle switch I like to get it .
Because it's default Boolean (true or false )so I'm trying to change it

I know full well what each of those lines do. What I'm saying is that putting those lines together doesn't do what you think it does.
You didn't even mention where that script is, and what triggers it...

But again, I don't think you need that script. I'm 98% sure there's a better solution to your problem...even not knowing what your problem actually is.
Which is why I asked, and I'm asking again, that you explain what you WANT to do, not HOW you're trying to do it.

edit:
image

No scripts here. No labels either. Only the built-in toggle's properties and one binding.

Please use the </> button to format your code properly as requested. We can't tell if you have the indents correct. It's also difficult to read your posts when you don't capitalise sentences correctly. Make it easy for those you are asking to help you. You can use the 🖉 edit link to fix your two previous posts.

1 Like