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:
...mytext = console.input_alert('Please enter text:')
The problem is basically that you're creating a new label in the
enternamefunction, 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 (
vin the code below), and use that like a
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.
load_view, each referenced function needs to have something happen to it.
load_viewneeds to do things to functions when it's called, so they have to exist at that time.