gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:35:28 25 4
gpt4 key购买 nike

我有一个字符串列表,我想在 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 作为变量名。它隐藏了内置函数/类型 list

关于python - 将列表打印到 Tkinter 文本小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com