1

Can you please take a look at this Tkinter code and let me know why I am not able to insert the values to correct columns?

as you can see from the attached image it seems it is adding empty string to first column!

from Tkinter import *
import ttk

root = 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,  values=("1A","1b"))

root.mainloop()

enter image description here

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Suffii
  • 5,694
  • 15
  • 55
  • 92

2 Answers2

2

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 Tk
import ttk


class 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()
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
1

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 ttk

root = 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()
Khristos
  • 973
  • 2
  • 11
  • 22