Forum Archive

newb question on printing output to a textview

buschfsu

new to python and programming but i have coded a ui that will give me the output i want (a string) when i press a button. i can include the print command in my buttons function and will print the string to the console but i can't find a way to populate the textview with this string. i have tried the following ...

def buttonGo(sender);

textview1 = ui.TextView()             # creates textview1 as a TextView
textview1.text = strlist                   # assigns textview1's text attribute to the string  'strlist'

fails with a message that i can edit ui.textview

very new to this but hope its something simple im overlooking.

ccc

What is the exact message that you receive?

What happens when you try this? : textview1.text = str(strlist)

Phuket2

I think @ccc said it best, because it should work.
I did a ugly example using a custom class. Don't want to confuse you, just trying to illustrate.

# coding: utf-8

import ui

class TestClass(ui.View):
    def __init__(self, frame):
        self.background_color = 'white' 
        self.frame = frame
        txt = ui.TextView(name = 'txtview')
        txt.text = 'from init...'
        txt.center = self.center
        self.add_subview(txt)

        btn = ui.Button(title ='change ui.Textview')
        btn.border_width = .5
        btn.x += 10
        btn.action = self.custom_action
        self.add_subview(btn)

    def custom_action(self, sender):
        self['txtview'].text = sender.title


if __name__ == '__main__':
    f = (0,0,540,576)
    tc = TestClass(f)
    tc.present('sheet')



buschfsu

ok i used the following within my defbuttonGo(sender);

textview1 = ui.TextView()
textview1.text = str(strlist)

print strlist

and i dont get an error. the print command works and gets the right string to the console but again the textview does not update with the string information, it stays blank

ccc

You want to create the TextView and do add_subview() only once.

The code above creates a new TextView each time the button is presses.

It would be better if you included button creation code too.

buschfsu

Ok here is the script. It grabs text from a csv file on an ftp server. If i put the textview outside of the button action the string is undefined. Sorry hope im being clear

# coding: utf-8

import ui
import csv
from  ftplib import FTP
from StringIO import StringIO

ftp = FTP('Server.com')
ftp.login()
ftp.set_pasv(True)
r= StringIO()
ftp.retrbinary('RETR record.csv',r.write)



def buttonGo(sender):

    alist = list()
    bandin = sender.superview['band']

    r.seek(0)
    csvfile=csv.reader(r.getvalue ().splitlines (),delimiter=',')

    for row in csvfile:
        if row[0] == bandin.text:
            alist.append(row[1])

    strlist = str()

    strlist = ',' .join(alist)

    textview1 = ui.TextView()
    textview1.text = str(strlist)

    print strlist



v = ui.load_view('vinyl q app')
if ui.get_screen_size()[1] >= 768:
    # 
    v.present('popover')
else:
    # iPhone
    v.present(orientations=['portrait'])
Phuket2

@buschfsu, it's clear what the problem is. In your def buttonGo, you say textview1 = ui.TextView().
That statement is just creating a new object of type ui.TextView
I am guessing on the view you load, called 'vinyl q app', you have a TextView defined.

So if you say:

# for simplicity get a reference to your TextView from your loaded view
tv= sender.superview['the_name_of_your_TextView']
# so you have a reference now, easy to use
tv.text = 'my new text'
tv.text_color = 'red'
# Etc....
# You also could just do 
sender.superview['the_name_of_your_TextView'].text  = 'my new text'
buschfsu

success!! thanks

i didnt know i needed

tv = sender.superview[ ]

but it makes sense when i look at what i did for the input text field
bandin

thanks!!!!