Facing Issue on Inserting Data to correct Column in Tkinter TreeView Facing Issue on Inserting Data to correct Column in Tkinter TreeView tkinter tkinter

Facing Issue on Inserting Data to correct Column in Tkinter TreeView


By coding:

tree = ttk.Treeview( root, columns=('Impacted Features'))

you created one column only.

Later in your program, you inserted data as if you had two columns:

tree.insert( "",0,  values=("1A","1b"))

So Tkinter creates an additional column without an empty heading. This is is exactly what your first screenshot shows.

So what can you do to design the GUI you highlighted in your second screenshot? Here is the answer:

  • Step one:

Create two columns:

tree = ttk.Treeview(root, columns=('zero', 'one'))

Note that I picked better symbolic names for the columns. This will lead you to display the data in the desired columns with the correct headings:

enter image description here

  • Step two:

But as you can see, there is a problem with this GUI: that empty space looks ugly. So to get rid of it, you need to rely on the show option:

tree['show'] = 'headings'

This will lead to the result you are looking for:

enter image description here

  • Step three:

In Python, everything is an object, so let me redesign your program so that you can scale it if you want:

import Tkinter as Tkimport ttkclass TreeViewDemo(Tk.Frame):    def __init__(self, master):        Tk.Frame.__init__(self, master)        self.master = master        self.master.title("Tree View Demo")            # The following 2 lings will expand the widgets as window resizes         # Can be removed if the effect is not desired        self.master.grid_rowconfigure(0,weight=1)        self.master.grid_columnconfigure(0,weight=1)        self.create_GUI()    def create_GUI(self):       self.tree = ttk.Treeview(self.master, columns=('zero', 'one'))       self.tree.heading('zero', text='Feature Class')       self.tree.heading('one', text='Impacted Features')       self.tree.column('zero')       self.tree.column('one')       self.tree.grid(row=0, column=0, sticky='nsew')       self.tree['show'] = 'headings'       self.tree.insert('', '0', values=("1A","1b"))def main():    root=Tk.Tk()    d = TreeViewDemo(root)    root.mainloop()if __name__ == '__main__':   main()


Change tree.insert( "",0, values=("1A","1b")) to tree.insert( "",0, text="1A", values=("1b"))

The first column or icon column (index #0) takes text and all other columns take values

Full code:

from Tkinter import *import ttkroot = Tk()tree = ttk.Treeview( root, columns=('Impacted Features'))tree.heading('#0', text='Feature Class')tree.heading('#1', text='Impacted Features')tree.column('#0', width=100)tree.column('#1', width=160)tree.grid(row=4, columnspan=6, sticky='nsew')tree.insert( "",0,  text="1A", values=("1b"))root.mainloop()