Forum Archive

2 (easy) Questions

NRWTV2

Hey! :)

  1. Question: I created a label and a button, i also created an event called "button_tapped", what i want is now that every time someone presses on this Button the Label changes via Label.Text, but he should get the old Value (for example 0) and add the an Amount (for example 2), the Button should show "2" now. :) Sry bad english

  2. Question: How am i able to react on an hud.password()? For example to get the Textfields Value?

Thy

lukaskollmer

What is hud.password()? Did you mean console.password_alert()?

AtomBombed

If you set your password alert in a variable, then you can use that variable for the TextField's text value. Example:

import console

myPassword = console.password_alert()

print myPassword

This would print out the password that the user inputted.

AtomBombed
def button_tapped(sender):
    previousAmount = sender.title # sets previous button's title in a variable.
    sender.title = previousAmount + 2 # adds 2 to the title, and displays it.
Cethric

To extend on @AtomBombed's answer I would also consider checking that previousAmount is stored as an integer or a float otherwise an error will be raised that type Int cannot be added to type str
Ie
```
def button_tapped(sender):
try:
previousAmount = int(sender.title)
except TypeError as e: # This will only make sure that 'sender.title' can be converted to a integer. It is not necessary if the title can always be converted
print e
previousAmount = 0
sender.title = previousAmount + 2

NRWTV2

No, its sadly just a "TypeError: coercing to Unicode: need string or buffer, int found

ccc

sender.title = str(previousAmount + 2)

Phuket2

@NRWTV2 , many ways to skin a cat as they say. This is one way. But many variations on this theme.
Most of the code below is just styling the objects. When you weed that out, there is very little being done

```

coding: utf-8

import ui

def btn_action(sender):
# sender is the button

# could do the below line, maybe not so clear... so will write it out
#the_value = int(sender.connected_label.text)
lb = sender.connected_label # dynamically created attribute 
int_val = int(lb.text)
the_value = int_val
print the_value
lb.text = str(the_value + sender.increment_value)

f = (0,0,200,200)
v = ui.View(frame = f)
v.bg_color = 'white'
lb = ui.Label(frame = (10,10,100, 32))
lb.text = '1001'
btn = ui.Button(frame = (10, lb.height + 30, 80, 32))
btn.title = 'Hit Me'
btn.action = btn_action
btn.border_width = .5
btn.corner_radius = 3
'''
very important, the 2 next lines of code are referencing attributes that dont exist in a ui.Button. They are created dynamically because you assign a value to them ( i think thats the right jargon)
'''
btn.connected_label = lb
btn.increment_value = 10

v.add_subview(btn)
v.add_subview(lb)
v.present('sheet')```

AtomBombed

@NRWTV2 ah yes. Sorry, I forgot to include (as @ccc said) that the added amount must then be converted to a string to be used as a title for button. sender.title = str(previousAmount + 2)