# Button Events onClick

**URL:** <https://forum.inductiveautomation.com/t/button-events-onclick/44163>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 5, 2021, 6:29pm UTC](https://forum.inductiveautomation.com/t/button-events-onclick/44163 "2021-03-05T18:29:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwilliams](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@jwilliams](https://forum.inductiveautomation.com/u/jwilliams)\
**Post date:** [March 5, 2021, 6:29pm UTC](https://forum.inductiveautomation.com/t/button-events-onclick/44163/1 "2021-03-05T18:29:59Z")

</div>

I have a button I am attempting to use events for a Mouse onClick event. The user clicks the button and it should swap between the text of Forward or Reverse. If it is Forward when they click, the button text should toggle to Reverse and vice versa. However, I cannot seem to get my python code to do that. It works Reverse \> Forward but not from Forward \> Reverse.  
Note that this is the simple input Button from Perspective Components.  
Here is my code, it should just be a simple if else statement but it only seems to work in one direction and I am uncertain why:

```auto
if system.tag.read(self.view.custom.Directionpath) == "false":
	system.tag.write(self.view.custom.Directionpath,"true")
else: 
	system.tag.write(self.view.custom.Directionpath,"false")

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 5, 2021, 6:41pm UTC](https://forum.inductiveautomation.com/t/button-events-onclick/44163/2 "2021-03-05T18:41:24Z")

</div>

`"true"` and `"false"` are defining String literals, not comparing booleans. Also, you’re reading a tag, which returns a `QualifiedValue`, not an actual value.  
Try:

```python
if system.tag.read(self.view.custom.Directionpath).value == False:
	system.tag.write(self.view.custom.Directionpath, True)
else: 
	system.tag.write(self.view.custom.Directionpath, False)

```

But that’s also a pretty inefficient way to do things - if it’s a boolean and you want to invert it:

```python
system.tag.write(self.view.custom.Directionpath, not system.tag.read(self.view.custom.Directionpath).value)

```

The `not` Python operator inverts a boolean.

---

<div class="post-metadata">

**Author:** ![jwilliams](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@jwilliams](https://forum.inductiveautomation.com/u/jwilliams)\
**Post date:** [March 5, 2021, 7:47pm UTC](https://forum.inductiveautomation.com/t/button-events-onclick/44163/3 "2021-03-05T19:47:50Z")

</div>

Using either set of code you sent back yields the same results as before. As long as the value is false (showing Reverse), I can toggle it to True (showing Forward). But once its set true, it will not toggle to false.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [March 5, 2021, 7:49pm UTC](https://forum.inductiveautomation.com/t/button-events-onclick/44163/4 "2021-03-05T19:49:35Z")

</div>

Maybe print out the value of the tag and its `type() ` to the console and check if it’s a boolean

---

<div class="post-metadata">

**Author:** ![jwilliams](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@jwilliams](https://forum.inductiveautomation.com/u/jwilliams)\
**Post date:** [March 5, 2021, 8:40pm UTC](https://forum.inductiveautomation.com/t/button-events-onclick/44163/5 "2021-03-05T20:40:12Z")

</div>

It is definitely a boolean, I am only trying to change this value here  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/1/d1c5e74d539b1157bae52508f1987428b58fdddf.png)  
and this one:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/8/a89ff70c0735ace72f349f4bab9aca6182a47e2b.png)  
It appears as though the scrip does not read the else part of the statement. I can swap the

```auto
if system.tag.read(self.view.custom.Directionpath) == "false":
	system.tag.write(self.view.custom.Directionpath,"true")
else: 
	system.tag.write(self.view.custom.Directionpath,"false")

```

to

```auto
if system.tag.read(self.view.custom.Directionpath) == "true":
	system.tag.write(self.view.custom.Directionpath,"false")
else: 
	system.tag.write(self.view.custom.Directionpath,"true")

```

and it only lets me swap it to false.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 5, 2021, 8:50pm UTC](https://forum.inductiveautomation.com/t/button-events-onclick/44163/6 "2021-03-05T20:50:53Z")

</div>

`system.tag.read()` returns a Qualified Value. You have to specify that you want the value

```auto
if not system.tag.read(self.view.custom.Directionpath).value:
    system.tag.write(self.view.custom.Directionpath,True)
else:
    system.tag.write(self.view.custom.Directionpath,False)

```

Also, you should consider moving to readBlocking and writeBlocking.
