Return multiple values in a For Loop

0

I am working on a project with Twitter which must be accessed through Tkinter, the fact is that I had first designed a function only in python, I used a 'For' loop and with print I saw what this gave me as result. The fact is that as a Tkinter widget I invoke the function I need that I return these results so they can appear in the GUI. It is known that when you see the first return the loop is finished, so I need to know a way for the function to return all the values I need without any problem. Next I put the code of the function and the part of the Tkinter, since I want to put this information in a Text widget. Thank you very much, I hope for a prompt response.

 def timeline():
   for status in tweepy.Cursor(api.home_timeline).items(2):
# Procesa el home timeline  
     return (status.user.name)
     return (status.user.screen_name)
     return (status.text.translate(non_bmp_map))

And I need to put that information in a text widget that looks like this:

 tweets=Text(stream,bg='Sky Blue',fg='white')
 tweets.pack(padx=10,pady=10 )
 tweets.config(state='normal')
 tweets.insert(END,timeline())
 tweets.config(state='disabled')
    
asked by Mauricio Sanabria Quesada 08.06.2018 в 03:28
source

1 answer

0

First, keep in mind that a function can only have a single return since it is the last clause that will be executed in the function.

Now, what you need is to return a value compatible with what the insert() method expects and is nothing other than a string. So we could make timeline() return that data. For example:

def timeline():
  cadena = ""
  for status in tweepy.Cursor(api.home_timeline).items(2):
    cadena = cadena + "{0} {1} {2}\n".format(status.user.name,status.user.screen_name,status.text.translate(non_bmp_map))

  return cadena

We basically go through the list and format a line with the three values separated by a space and with a line end at the end.

    
answered by 08.06.2018 / 04:32
source