将列表打印到Tkinter文本小部件


问题内容

我有一个字符串列表,我想在 Tkinter 文本小部件中打印这些字符串,但是我不能在新行中插入每个字符串。

我试过了但是没用:

ls = [a, b, c, d]

for i in range(len(lst)):
    text.insert(1.0+i lines, ls[i])

问题答案:

'\n'手动添加换行符():

from Tkinter import *  # from tkinter import *

lst = ['a', 'b', 'c', 'd']

root = Tk()
t = Text(root)
for x in lst:
    t.insert(END, x + '\n')
t.pack()
root.mainloop()

顺便说一句,您不需要使用索引来迭代列表。只是迭代列表。并且不要将其list用作变量名。它遮盖了内置的function / type list