Forum Archive

How i make UI buttons and link them?

Abdalwahab

Hi I want to make a simple UI that change a label text to a text I input when I press a button

scopaest

Idk if this is the best solution, but u could do it this way:

(0. create the button and the label with the ui designer...)
1. click on the 'i' to edit the button
2. set in the field "Action" a name (button1_Action, or what ever..)
3. in the Code/py-File u have now to define the button and say, what it should do when clicked:

def button1_Action(sender):
    sender.superview['label1'].text = 'new Text'

edit: I just saw that u want to set the text from a Text-input.
in this case u have to expand it:
instead of:

def button1_Action(sender):
    sender.superview['label1'].text = 'new Text'

code this:

sender.superview['label1'].text = sender sender.superview['textfield1'].text

(or if u want to do it hard-coded: see post @ccc )

JonB

some other resources for learning the ui module: the calculator example in the Examples/User Interface folder.
Humberry's UI module tutorial

ccc
import ui
label = ui.Label(text='Label', text_color = 'red')

def field_action(sender):
    label.text = sender.text

field = ui.TextField(text='Field', action=field_action, enabled=False)
field.y = label.y + label.height + 10

def button_action(sender):
    field.enabled = not field.enabled

button = ui.Button(title='Button', action=button_action)
button.y = field.y + field.height + 10

view = ui.View()
for subview in (label, field, button):
    view.add_subview(subview)
view.present()
ccc

A more responsive class-based approach...

import ui

class MyView(ui.View):
    def __init__(self):
        label = ui.Label(name='label', text='Label', text_color = 'red')
        field = ui.TextField(name='field', text='Field', delegate=self, enabled=False)
        field.y = label.y + label.height + 10
        button = ui.Button(title='Disabled', action=self.action)
        button.y = field.y + field.height + 10
        for subview in (label, field, button):
            self.add_subview(subview)
        self.present()

    def action(self, sender):
        self['field'].enabled = not self['field'].enabled
        sender.title = 'Enabled' if self['field'].enabled else 'Disabled'

    def textfield_did_change(self, textfield):
        self['label'].text = textfield.text

MyView()
ccc
import ui

locked = ui.Image.named('iow:locked_32')
unlocked = ui.Image.named('iow:unlocked_32')
view = ui.View()
field = ui.TextField(text='Field', enabled=False)
view.add_subview(field)

def action(sender):
    field.enabled = not field.enabled
    sender.image = unlocked if field.enabled else locked

view.add_subview(ui.Button(action=action, image=locked, name='button'))
view['button'].x = field.x + field.width + 10
view.present()