Printing a list to a Tkinter Text widget

17,516

Solution 1

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.

Solution 2

You can also create a frame and pack (or grid) labels to it:

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

This allows you to manage list items separately if you keep a reference (which I didn't).

Share:
17,516
HBasiri
Author by

HBasiri

Updated on June 04, 2022

Comments

  • HBasiri
    HBasiri almost 2 years

    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])
    
  • Russell Smith
    Russell Smith over 9 years
    You can also use a single insert statement along with '\n'.join(lst)