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()
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()
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:
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:
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:
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.