Printing a list to a Tkinter Text widget

前端 未结 1 962
有刺的猬
有刺的猬 2021-01-13 16:40

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\

相关标签:
1条回答
  • 2021-01-13 16:54

    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.

    0 讨论(0)
提交回复
热议问题