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.


    help me figure out the UI Textfield

    Pythonista
    3
    4
    770
    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.
    • alexandrsemenov
      alexandrsemenov last edited by

      Hello everyone, help please, I myself am not very good at programming, I need to enter text into the textfield window, write it to a variable and pass it to the next function, please show a small example

      cvp 1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        from ui import View, TextField, Label
        from collections import deque
        
        ''' simple conversation bot:
           
           prompt label
           [__textfield__________]
                    
        '''
        
        #create a 400x400 view to be our container
        v=View(frame=(0,0,400,400), bg_color='white')
        
        #create a prompt label
        prompt=Label(frame=(0,0,400,44))
        
        #create an empty textfield
        tf=TextField(frame=(0,50,400,44))
        
        #add the elements to the view
        v.add_subview(prompt)
        v.add_subview(tf)
        
        '''the concept here is to create a sequence of callbacks.  each callback must respond to the text in the textbox, and take some other action, like updating the propmpt in response, storing variables, then setting up for the next action.  '''
        
        #just to allow creations of pactions that we can cycle through
        action_queue=deque()
        
        def next_action():
           '''load the next action into the textfield'''
           tf.action=action_queue.popleft()
           action_queue.append(tf.action)
           
        def ask_name(sender):
           ''' ask the user for his/her name'''
           prompt.text='What is your name'
           tf.text=''
           next_action() 
           
        def ask_age(sender):
           '''called in response to ask_name prompt, respond with the name and ask age'''
           username=tf.text
           tf.text=''
           prompt.text='Hello, {} how old are you?'.format(username)   
        
           #save this for later.  a lazy but easy approach is to stash attributes in your ui components.  
           tf.username=username
           next_action()
        
        def comment_on_age(sender):   
           '''use previously stored name variable'''
           try:
              age=int(tf.text)
           except ValueError:
              prompt.text='Huh? Age is a just a number, what is yours?'
              tf.text=''
              return
              
           tf.text=''
           if age < 20:
              prompt.text='You whole life is ahead of you, {}'.format(tf.username)
           elif age < 40:
              prompt.text='These are your best years, enjoy them'
           elif age >= 40:
              prompt.text='Is it all downhill from here, {}?'.format(tf.username)
           
           next_action()
        
        #now, setup our sequence of actions, then call the first action to start us off. 
        action_queue.extend([ask_name, ask_age, comment_on_age])  
        next_action()   
        tf.action(tf)
        
        
        v.present('sheet')
        
        
        

        This is probably a bad example, since this is more of a dialog than a TextField.

        cvp 1 Reply Last reply Reply Quote 0
        • cvp
          cvp @JonB last edited by cvp

          @JonB If he is a novice in programmation, you will push him to suicide.😂

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp @alexandrsemenov last edited by cvp

            @alexandrsemenov if @JonB 's solution is too complex for your request, please try

            import ui
            
            tf = ui.TextField()
            tf.frame = (0,0,300,40)
            tf.name = 'Window of TextField'
            tf.placeholder = 'type here, then press X'
            tf.begin_editing()
            tf.present('sheet')
            tf.wait_modal()
            
            var = tf.text				# put the TextField text in a variable
            def next_function(v):
            	print(v)
            next_function(var)	# pass the variable to the next function
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors