I have a list of strings, and I want to print those strings in a Tkinter text widget, but I can’t insert each string in a new line.
I tried this but didn’t work:
JavaScript
x
5
1
ls = [a, b, c, d]
2
3
for i in range(len(lst)):
4
text.insert(1.0+i lines, ls[i])
5
Advertisement
Answer
Append newline ('n'
) manually:
JavaScript
1
11
11
1
from Tkinter import * # from tkinter import *
2
3
lst = ['a', 'b', 'c', 'd']
4
5
root = Tk()
6
t = Text(root)
7
for x in lst:
8
t.insert(END, x + 'n')
9
t.pack()
10
root.mainloop()
11
BTW, you don’t need to use index to iterate a list. Just iterate the list. And don’t use list
as a variable name. It shadows builtin function/type list
.