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.


    2 (easy) Questions

    Pythonista
    6
    9
    4520
    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.
    • NRWTV2
      NRWTV2 last edited by

      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

      Phuket2 1 Reply Last reply Reply Quote 0
      • lukaskollmer
        lukaskollmer last edited by

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

        1 Reply Last reply Reply Quote 2
        • AtomBombed
          AtomBombed last edited by

          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.

          1 Reply Last reply Reply Quote 1
          • AtomBombed
            AtomBombed last edited by 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.
            
            1 Reply Last reply Reply Quote 2
            • Cethric
              Cethric last edited by

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

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

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

                  sender.title = str(previousAmount + 2)

                  1 Reply Last reply Reply Quote 1
                  • Phuket2
                    Phuket2 @NRWTV2 last edited by

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

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