omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Can you assign a button input to a variable

    Pythonista
    3
    9
    3152
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • NewbieCoder
      NewbieCoder last edited by

      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 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @NewbieCoder last edited by

        @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')
        
        1 Reply Last reply Reply Quote 0
        • ccc
          ccc last edited by 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 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @ccc last edited by

            @ccc markup for documentation tool see here

            1 Reply Last reply Reply Quote 1
            • NewbieCoder
              NewbieCoder last edited by

              Its just the prerson that tought me

              1 Reply Last reply Reply Quote 0
              • ccc
                ccc last edited by 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

                1 Reply Last reply Reply Quote 0
                • NewbieCoder
                  NewbieCoder last edited by

                  Why you bully me)))

                  1 Reply Last reply Reply Quote 0
                  • NewbieCoder
                    NewbieCoder last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • ccc
                      ccc last edited by

                      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.

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post
                      Powered by NodeBB Forums | Contributors