Python 将列表打印到Tkinter文本小部件
我有一个字符串列表,我想在Tkinter文本小部件中打印这些字符串,但我无法将每个字符串插入新行 我试过了,但没有成功:Python 将列表打印到Tkinter文本小部件,python,text,tkinter,widget,Python,Text,Tkinter,Widget,我有一个字符串列表,我想在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 =
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
作为变量名。它隐藏内置函数/类型列表
您还可以创建框架并将标签打包到其中:
from tkinter import *
root = Tk()
myFrame = Frame(root).place(x=50, y=100)
myList = ["a", "b", "c"]
for i in myList:
Label(myFrame, text = "● "+i).pack() #you can use a bullet point emoji.
root.mainloop()
这允许您在保留引用(我没有)的情况下单独管理列表项。您还可以使用单个
insert
语句和'\n'.join(lst)