Skip to content Skip to sidebar Skip to footer

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]

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

Post a Comment for "Printing A List To A Tkinter Text Widget"