0

I'd like to insert data into a specific column of my ttk treeview widget. The users only input is a scanner, so I have to parse each code to determine which column it goes into. Right now, the parseData() function runs on pressing Enter, so I would like to take care of the insert there.

I have tried using the treeview.set(item, column, new_value), but it falls apart when inserting into a blank row.

class MainGUI:
    def __init__(self, master):
        self.master = master
        self.data_view = ttk.Treeview(master)
        self.data_view['columns'] = ["Date", "Item 1", "Item 2", "Item 3", "Item 4", "Source", "Destination", "Cart #",]
        self.data_view['show'] = 'headings'
        self.data_view.heading("Date", text="Date")
        self.data_view.heading("Item 1", text="Item 1")
        self.data_view.heading("Item 2", text="Item 2")
        self.data_view.heading("Item 3", text="Item 3")
        self.data_view.heading("Item 4", text="Item 4")
        self.data_view.heading("Source", text="Source")
        self.data_view.heading("Destination", text="Destination")
        self.data_view.heading("Cart #", text="Cart #")
        self.data_view.pack()

        self.scan_entry = ttk.Entry(master)
        self.scan_entry.pack()
        self.scan_entry.bind('<Return>', self.parseEntry)
        self.scan_entry.focus()

        self.close_button = tk.Button(master, text="Close", command=self.adminLogout, width=15, height=5)
        self.close_button.pack(anchor='se')

    def parseEntry(self,event):
        #INSERT self.scan_entry.get() into #ITEM 1 column, not date volumn
        #If contains certain characters, maybe insert into Source or Destination
        self.scan_entry.delete(0,'end')

Ideally, I'd like to be able to insert into the blank tree to any column, or in some cases grab what is selected, then change the value based on input.

1
  • 1
    Is self.data_view.insert('', 'end', values=('', self.scan_entry.get())) what you want? Commented Jan 15, 2019 at 22:29

1 Answer 1

1

You will have to insert empty in the columns you do not want to fill.

And you can use in to check for the character you seek in self.scan_entry.get():

from tkinter import *
import tkinter.ttk as ttk
class MainGUI:
    def __init__(self, master):
        self.master = master
        self.data_view = ttk.Treeview(master)
        self.data_view['columns'] = ["Date", "Item 1", "Item 2", "Item 3", "Item 4", "Source", "Destination", "Cart #",]
        self.data_view['show'] = 'headings'
        self.data_view.heading("Date", text="Date")
        self.data_view.heading("Item 1", text="Item 1")
        self.data_view.heading("Item 2", text="Item 2")
        self.data_view.heading("Item 3", text="Item 3")
        self.data_view.heading("Item 4", text="Item 4")
        self.data_view.heading("Source", text="Source")
        self.data_view.heading("Destination", text="Destination")
        self.data_view.heading("Cart #", text="Cart #")
        self.data_view.pack()
        self.scan_entry = ttk.Entry(master)
        self.scan_entry.pack()
        self.scan_entry.bind('<Return>', self.parseEntry)
        self.scan_entry.focus()

        self.close_button = ttk.Button(master, text="Close", command=lambda:print('want to close app'))
        self.close_button.pack(anchor='se')

    def parseEntry(self,event):
        #self.scan_entry.delete(0,'end')
        self.data_view.insert('',END,values=('',self.scan_entry.get(),'','','','','',''))
        #If contains certain characters, maybe insert into Source or Destination       
        if 'a' in self.scan_entry.get():
            self.data_view.insert('',END,values=('','','','','',self.scan_entry.get(),self.scan_entry.get(),''))


if __name__=='__main__':
    master=Tk()
    maingui=MainGUI(master)
    master.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

In your code each value create a new row at the end is it possible to insert the new value row at the top of the table inside a specific column even if i have already inserted few rows to the table.
Yes. Use the iid argument to give each row a unique ID when you are initially creating it, then you can later reference that iid and the respective column to insert data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.