Skip to content
Advertisement

Printing a list to a Tkinter Text widget

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:

ls = [a, b, c, d]

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

Advertisement

Answer

Append newline ('n') manually:

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

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement