Python Forum

Full Version: TreeView column headers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all
I have posted a code section of a larger program where im trying to populate a a treeview with 2 columns and inset 2 values.
My issue is the treeview will show but the 2 column headers will not display as well as the 2 values.
thank you for any direction

# Introduction to Python Programming
from tkinter import *
import shelve
from tkinter import ttk
class MyFrame (Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.geometry("600x600")
        self.master.title("Student Scores")
        self.grid()
           
        #Create and define a TreeView
        columns = ("student", "score")
        self.treeview = ttk.Treeview(self, columns = columns, show = "headings")
        self.treeview.column("student",anchor=CENTER, stretch=NO, width=20)
        self.treeview.heading("student", text="Student")
        self.treeview.column("score",anchor=CENTER, stretch=NO, width=20)
        self.treeview.heading("score", text="Score")
        self.treeview = ttk.Treeview(self, height = 15)
        self.treeview.grid(padx = 50, pady = 50)
        #insert values to column
        self.treeview.insert('', 'end', text="Student", values=('Jon', '99'))
        
asn_frame = MyFrame()
asn_frame.mainloop()
I've uploaded a bare bones basics of all you need to do to create a treeview. I hope it helps.
import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title('Student Scores')
root.geometry('600x600')
cols = ("student", "score")
treeview = ttk.Treeview(root, columns = cols, show="headings")
treeview.heading ('student', text='Student')
treeview.heading ('score', text="Scores")
treeview.insert ('', tk.END, values = ('Micky Mouse', '100'))
treeview.insert ('', tk.END, values = ('Bugs Bunny', '81'))
treeview.grid (row=0, column=1)
root.mainloop ()
Thank you this was very helpful. I have one tweak I would like to make, this code will open up a 2nd window on which contains the TreeView, I would like the view to launch on the primary window where I would have all the other controls
thanks