Forum Archive

Button action to increment += 1 a label value

168grani

Good morning to all guys,

I'm quite new to Python and absolutely new to Pythonista.

I'm a volleyball coach and I'm trying to build by myself a simple scouting app.
I'm trying to create a function for a button to be able to increment by 1 the value shown inside a label on the GUI.
The buttons has a title different to 1, they must indicate the related attack position inside the playing field.

Can anyone help me?

Scuse me if the question is stupid and thanks you all prior.

JonB

A button can have other user defined attributes, so you can have the name of the label to be updated for example. You can also assign button a name, and then define the label name as the same as the button name, except with Label at the end...


def button_action(sender):
   labelName=sender.name+'Label' 
   sender.parent[labelName].text= str(int(sender.parent[labelName].text)+1)

V=ui.View()
B=ui.Button(title='center', name='center')
L=ui.Label(text="0", name='centerLabel')
B.action=button_action
# ...

Or you could even store the label itself as an attribute of the button...

def button_action(sender):
   sender.label.text= str(int(sender.label.text)+1)

V=ui.View()
B=ui.Button(title='center', name='center')
L=ui.Label(text="0", name='centerLabel')
B.label=L
B.action=button_action
# ..

Alternatively, store the number as a custom button attribute, and have a separate function that updates all of the labels.

Eg. The action would simply be:

sender.count+=1
update_labels()

Then a separate function that maps each label to each button value.