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.


    Change the text of a label in the UI?

    Pythonista
    4
    5
    8622
    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.
    • nasadave
      nasadave last edited by

      Fairly new to python here, so be gentle…

      I am trying to change the text of a label on a UI based on user input into a console alert input variable. I can get a button title to change, but not a label text. What am I missing here?

      This is basically what I'm doing (textlabel is the name of the label box in the UI). The program seems to run fine but makes no change to the label text:

      def entername():

      ...mytext = console.input_alert('Please enter text:')

      ...textlabel=ui.Label('textlabel')

      ...textlabel.text=mytext

      ui.load_view('myUI').present('sheet')

      entername()

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

        The problem is basically that you're creating a new label in the entername function, one that is actually never visible on screen. To access the label that is contained in the UI file you're loading, you have to keep a reference to the loaded view (v in the code below), and use that like a dict:

        import ui
        import console
        
        v = ui.load_view('myUI')
        v.present('sheet')
        
        def entername():
        	mytext = console.input_alert('Please enter text:')
        	textlabel = v['textlabel']
        	textlabel.text = mytext
        
        entername()
        
        1 Reply Last reply Reply Quote 0
        • nasadave
          nasadave last edited by

          Genius, exactly what I needed, thank you!

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

            So just to get it right: any action listener, must be defined before ui.load_view but things that change elements of the view must be placed beneath it?

            Webmaster4o 1 Reply Last reply Reply Quote 0
            • Webmaster4o
              Webmaster4o @vcr80 last edited by

              @vcr80 Any functions that are referenced inside the pyui file need to be defined before loading. If you manually bind an action after loading the pyui file, then you can define that after.

              When using load_view, each referenced function needs to have something happen to it. load_view needs to do things to functions when it's called, so they have to exist at that time.

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