Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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])
like image 722
HBasiri Avatar asked Feb 11 '23 13:02

HBasiri


1 Answers

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.

like image 169
falsetru Avatar answered Feb 16 '23 04:02

falsetru