Search code examples
pythonpython-3.xtkintertreeviewttk

tkinter - ttk treeview: see column text


I'm building a table in Tkinter using ttk's Treeview widget. However, after I inserted the columns they display it without text. Here is the code:

w=Tk()
f=Frame(w)
f.pack()
t=Treeview(f,columns=("Titolo","Data","Allegati?"))
t.pack(padx=10,pady=10)
t.insert("",1,text="Sample")

Here the result:

Treeview Result Image

How can I solve?

Thanks


Solution

  • You need to define the headers for each column. I don't know if you want to use the same column names for the header or not so that is going to be my example. You can change the text to whatever you want. To define the header you will need to use heading() like this:

    t.heading("Titolo", text="Titolo")
    t.heading("Data", text="Data")
    t.heading("Allegati?", text="Allegati?")
    

    With those changes your final code should look like this:

    from tkinter import *
    from tkinter.ttk import *
    
    
    w=Tk()
    
    f = Frame(w)
    f.pack()
    t = Treeview(f, columns=("Titolo", "Data", "Allegati?"))
    
    t.heading("Titolo", text="Titolo")
    t.heading("Data", text="Data")
    t.heading("Allegati?", text="Allegati?")
    
    t.pack(padx=10, pady=10)
    t.insert("", 1, text="Sample")
    
    w.mainloop()
    

    Results:

    enter image description here

    Let me know if you have any questions.