Forum Archive

Hard coding ui, gathering and using user input

Taje

I find myself lost and in need of help. I've looked through various examples of code that teaches one how to take user input, yet I've noticed they all pretty much use the ui designer. True the ui designer is great, I tend to prefer to hard code mainly to get more experience at being able to code in any interpreter such as the original Python interpreter.

So how do I go about gathering and using user input?

In this one I'm trying to go along with an example i found with-in a form which some questioned how to take input using text field.

# coding: utf-8


import ui
w, h = ui.get_screen_size()


# action

def tex_act(sender):

    sender.superview["texting"].text

# view

the_view = ui.View()

# text field

texting = ui.TextField()

texting.frame = (0, 0, 320, 80)

texting.placeholder = "Here: "

texting.action = tex_act

# text view

view_text = ui.TextView()

view_text.frame = (0, 90, 320, 300)

view_text.editable = False

# add subview

the_view.add_subview(texting)

the_view.add_subview(view_text)


# present

the_view.present("sheet")

Other example of my failure this more so how I'm trying to write my scripts.

# coding: utf-8


import ui


def text_field_action(sender):
    sender.superview = view_text.text


def my_views():


    # view

    the_view = ui.View()


    # text field 

    texting = ui.TextField()

    texting.name = "texting"

    texting.frame = (0, 0, 320, 50)

    texting.placeholder = "here: "


    # text view

    view_text = ui.TextView()

    view_text.name = "view_text"

    view_text.editable = False

    view_text.frame = (0, 70, 320, 360)


    # text action

    texting.action = text_field_action

    # add subview
    the_view.add_subview(view_text)
    the_view.add_subview(texting)


    # present

    the_view.present("sheet")


    # run main

def main():

    my_views()

if __name__ == "__main__":
    main()

I truly could appreciate any help, guidance, suggestions, and tips regarding Pythonista, python, and how to properly writing my scripts. As I see many of you have far more experience and greater talent at this than myself. Thank you.

tlinnet

Hi.

I have made a script, which works both in Python in terminal and through pythonista.

The start of the script figures out, "how" it is running.
So I can test functions at terminal, and slowly expanding the GUI.

Have a look here:
https://github.com/tlinnet/hotspotsystem/tree/master/api/pythonista

Taje

@tlinnet thank you I will indeed check it out and try to learn from it.

ramvee

I am also a beginner and asked a similar question. @omz the developer of pythonista, said we have to declare the name of the UI component explicitly. I have altered your first script accordingly and it now works. :)

# coding: utf-8

import ui
w, h = ui.get_screen_size()

# action

def tex_act(sender):

    view_text.text = sender.text
    #sender.superview["texting"].text

# view
the_view = ui.View()

# text field
texting = ui.TextField()

texting.frame = (0, 0, 320, 80)

# explicit name declaration is essential
texting.name = 'texting'

texting.placeholder = "Here: "

texting.action = tex_act

# text view

view_text = ui.TextView()

view_text.frame = (0, 90, 320, 300)

# explicit name declaration is essential
view_text.name = 'view_text'

view_text.editable = False

# add subview

the_view.add_subview(texting)

the_view.add_subview(view_text)

# present
the_view.present("sheet")
ramvee

And in your second attempt, you just have to change the line:
sender.superview = view_text.text
to:
sender.superview['view_text'].text = sender.text

Taje

@ramvee thank you I see the difference and appreciate your post that helps thanks!

Phuket2

@Taje , another way could be to use the dialogs module. dialogs.form_dialog(title='', fields=None, sections=None) is a handy way to collect user input. Maybe it's too static for what you need. But regardless it's a python file distributed by @omz. You can find the source in the site-packages dir. it's just called dialogs.py