tkinter 输出 returns 字符串作为字典

tkinter output returns strings as a dict

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()

其中 returns this.
如何使输出看起来更好:
第一行
第二行

我尝试将 message 获取为列表(i for i in output()),但结果是一样的。

您不需要列表!只是 join 生成器的内容作为字符串:

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

而且根本没有听写,让花括号不打扰你。尝试打印您的列表:

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

所以如果你真的喜欢列表,你可以 join 你的 return 列表:

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

另一个注意事项,如您所见,您可以像对待列表一样对待生成器,但这并不完全正确,试试这个:

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

如您所见,它是某种临时列表,可让您一次删除每个项目或一次删除所有项目。所以如果你想保留信息——列表是个好主意!

更多here.