Forum Archive

Struggling to UI a grid of fields

AlanEdwards

Hello all, totally new to Python and Pythonista although I do have experience of AppleScript.

First up kudos to Ole, fantastic app.

Second up is my head scratcher. I'm trying to create a UI with a grid of uniquely identifiable text fields. I don't think I can use a PYUI as I need a running digital clock on the screen at the same time, so I figured I would need to create the UI screen in the code.

My code to make the first column makes a text box for each iteration through the loop but only the last one displays. What can I add/change to get the earlier ones to display?

Thanks, Alan

import ui

def fieldEdited(sender):
t=view[fieldName].text
print t

for loop in range(1,11):
fieldName='field' + str(loop)

view = ui.View()                                    
view.background_color = 'white'

field = ui.TextField()

field.name='field'+str(loop)
field.text= fieldName
field.center = (70,(90+loop*30)) 
field.height=27
field.width=82
field.action = fieldEdited

view.add_subview(field)

view.present(style='full_screen',hide_title_bar='True')

AlanEdwards

Sorry that was untidy, I'll try again

````import ui

def fieldEdited(sender):
t=view[fieldName].text
print t

for loop in range(1,11):
#print loop
fieldName='field' + str(loop)

view = ui.View()                                    
view.background_color = 'white' 
                 
field = ui.TextField()

print fieldName
field.name='field'+str(loop)
field.text= fieldName
field.center = (70,(90+loop*30)) 
field.height=27
field.width=82
field.action = fieldEdited

view.add_subview(field)  

view.present(style='full_screen',hide_title_bar='True')````

AlanEdwards

Oh! just someone shoot me.

polymerchm

when you post code, prefix and postfix it all with three backticks. If your prefix has the word python immediately after it, you color coding. Much easier to read.

AlanEdwards

```import ui

def fieldEdited(sender):
t=view[fieldName].text
print t

for loop in range(1,11):
#print loop
fieldName='field' + str(loop)

view = ui.View()                                    
view.background_color = 'white' 
                 
field = ui.TextField()

print fieldName
field.name='field'+str(loop)
field.text= fieldName
field.center = (70,(90+loop*30)) 
field.height=27
field.width=82
field.action = fieldEdited

view.add_subview(field)  

view.present(style='full_screen',hide_title_bar='True')```

AlanEdwards

Please polymerchm could you save me?!

polymerchm

In your action, sender.name returns the name of the textview you have edited.

polymerchm

put the first three back ticks on their own line immediately followed by python. After all the code, on its own line, three more backticks

AlanEdwards

Sorry it just displays very badly, it does not like my Apple Mac text.

ccc
blank line
three backticks python<return!!!>
your code
three backticks

You can edit any of your existing posts by clicking edit at the upperright of the post.

AlanEdwards
import ui

def fieldEdited(sender):
    t=view[fieldName].text
    print t

for loop in range(1,11):
    #print loop
    fieldName='field' + str(loop)


    view = ui.View()                                    
    view.background_color = 'white' 
                     
    field = ui.TextField()

    print fieldName
    field.name='field'+str(loop)
    field.text= fieldName
    field.center = (70,(90+loop*30)) 
    field.height=27
    field.width=82
    field.action = fieldEdited                      

view.add_subview(field)                          

view.present(style='full_screen',hide_title_bar='True')
AlanEdwards

Thank you ccc, who would have thought I could make such a mess of it?

Now can anybody solve my original problem, it's a long way back.

polymerchm
import ui

def fieldEdited(sender):
    t=sender.text   # note change
    print t

view = ui.View()
view.background_color = 'white'


for loop in range(1,11):
    #print loop
    fieldName ='field' + str(loop)                     
    field = ui.TextField()
    field.name = fieldName
    field.text= 'type something'
    field.center = (70,(90+loop*30)) 
    field.height=27
    field.width=82
    field.action = fieldEdited                      
    view.add_subview(field)                          

view.present(style='full_screen',hide_title_bar='True')
AlanEdwards

Wow, thanks. A simple as that, and no doubt after a lot of experience on your part. Thanks.