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?
-
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()
-
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 adict
: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()
-
Genius, exactly what I needed, thank you!
-
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?
-
@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.