0

I have a SQLite database that I am going to read from and list one of its tables data into a tree view. I have been searching for a long time in order to get this to work and I am struggling to find anything that either works or makes sense to me. for instance in my table I have the headings 'Member ID' and 'Full Name'.

for test purposes I have created variables storing strings for these values.

root = Tk()

name = "cameron"
id="223344"

lblsearchName = Label(root, text="Full Name:")
lblsearchName.grid(sticky=E)
searchEntry = Entry(root)
searchEntry.grid(column=1, sticky=E)

treeView = ttk.Treeview(root)
treeView.grid(columnspan=2)

root.mainloop()

How do I go about creating headings in the treeview according to the headings in my tables of my database? I now how to read for the database but I then need to know how I would insert this values into the treeview. (for this example 'name' and 'id')

Cameron Hamilton
  • 29
  • 1
  • 2
  • 8

1 Answers1

4
# set up the columns and headings
# In reality "Member ID" would be exported from the database
treeview["columns"] = ["Member ID", "Full Name"]
treeview["show"] = "headings"
treeview.heading("Member ID", text="Member ID")
treeview.heading("Full Name", text="Full Name")

# Add content using (where index is the position/row of the treeview)
# iid is the item index (used to access a specific element in the treeview)
# you can set iid to be equal to the index
tuples = [(1, "Name1"),(2, "Name2")]
index = iid = 0
for row in tuples:
    treeView.insert("", index, iid, values=row)
    index = iid = index + 1

Sample output:

screenshot showing output code creates

More information on heading.

More information on insert.

More information on options (E.g. columns and headings)

martineau
  • 119,623
  • 25
  • 170
  • 301
Roars
  • 623
  • 5
  • 17
  • Had an error. 'C:\Users\User\AppData\Local\Programs\Python\Python36\python.exe C:/Users/User/OneDrive/PycharmProjects/TKinter/treeView.py File "C:/Users/User/OneDrive/PycharmProjects/TKinter/treeView.py", line 18 treeView.["show"] = "headings" ^ SyntaxError: invalid syntax' – Cameron Hamilton Nov 27 '17 at 16:18
  • yeah sorry, accidentally added a '.' while copying. Should be fixed. – Roars Nov 27 '17 at 16:19
  • 2
    Brilliant it worked! Thank you very much. Would you just be able to elaborate on index and iid just a little I'm still unsure what they exactly are. I set them to 0,1 which worked fine but that was a lucky guess. – Cameron Hamilton Nov 27 '17 at 16:25
  • index is the position/row in which the information will be placed. iid is the item identifier given to the information, which can be used to access it. If you set this equal to the index, you can basically use the index to access the information when using other treeview methods. For example, you use the iid to delete rows. – Roars Nov 27 '17 at 16:26
  • Okay so, when I fetch the data from the database it gives me multiple tuples. How would I then insert each element of this tuple into the tree view. (sorry to bother you again) – Cameron Hamilton Nov 27 '17 at 16:51
  • For instance for each user it returns their member id, full name, password. all in one tuple, then follows with another tuple for the next user. How do I insert each user's info into treeview – Cameron Hamilton Nov 27 '17 at 16:57
  • No worries I have figured it out – Cameron Hamilton Nov 27 '17 at 17:52