Forum Archive

Can you assign a button input to a variable

NewbieCoder

Im having trouble trying to figure out how to assign a button press to a variable and at first i tried this code

def button_tapped(sender):
'@type sender: ui.Button'

    a = sender.title

But it obviously did not work
Please help

cvp

@NewbieCoder If you want to store the title of the tapped button in a variable:

import ui

v = ui.View()
v.frame = (0,0,400,400)
v.name = 'test'

b = ui.Button()
b.title = 'My Title'
b.background_color = 'white'
b.border_color = 'blue'
b.border_width = 1
b.corner_radius = 5
b.frame = (10,10,100,32)
a = ''
def tap(sender):
    a = sender.title
    print(a)    
b.action = tap
v.add_subview(b)

v.present('sheet')
ccc

@NewbieCoder A great answer has been provided by @cvp but I have a different question... Where does the syntax '@type sender: ui.Button' come from? I have not seen that before.

Python 3’s builtin function annotation syntax would be something like def tap(sender: ui.Button) -> None:

cvp

@ccc markup for documentation tool see here

NewbieCoder

Its just the prerson that tought me

ccc

Wikipedia says: “The project Epydoc is inactive since February 2009.”.

So, time to drop Epydoc and learn Python’s builtin type annotations... http://www.mypy-lang.org

NewbieCoder

Why you bully me)))

NewbieCoder

@ccc i set my phones date to Декабрь 2008)))))))))))

ccc

I have no intention to bully you. Only trying to help. Python has a builtin set of capabilities (help(), pydoc, inspect, typing) that will help generate epydoc-like documentation of your code as well as do mypy type checking to find errors. The code that you write will be more future proof and readable by others if it follows the standard.