0
from tkinter import *
from tkinter.messagebox import showinfo

def output():

    yield 'The %s line' % ('first')
    yield '\n'
    yield 'The %s line' % ('second')


def reply():
    showinfo(title='Log size', message=(list(output())))

window = Tk()
button = Button(window, text='Get info', command=reply)
button.pack()
window.mainloop()

which returns this.
How can I make the output looks better as:
The first line
The second line

I have tried to get message as list(i for i in output()), but the result is the same.

feedthemachine
  • 592
  • 2
  • 11
  • 29

1 Answers1

1

You don't need a list! Just join generator's content as string:

def reply():
    showinfo(title='Log size', message=''.join(output()))

And there's no dicts at all, let the curly braces not bother you. Try to print your list:

>>> list(output())
['The first line', '\n', 'The second line']

So if you really loves lists, you can join your return list:

def reply():
    showinfo(title='Log size', message=(''.join(list(output()))))

Another note, as you see that you can treat generators like list, but it's not completely true, try this:

def reply():
    gen = output()
    showinfo(title='Log size', message=''.join(gen))
    showinfo(title='Log size', message=''.join(gen))

So as you see, it's some sort of temporary list, that lets you take off each item one at a time or all of them only once. So if you want to preserve information - list is a good idea!

More here.

Community
  • 1
  • 1
CommonSense
  • 4,232
  • 2
  • 14
  • 38