tkinter Treeview widget inserting data
You seem to be interested only in how to insert data the user types within Tkinter.Entry()
widgets into ttk.Treeview()
after a Tkinter.Button()
click.
I designed a simple interface to show you how to resolve this. You can adapt my solution to your problem.
Here is how the application demo looks like:
So I set a counter self.i
to name the items. But you can add a label and an entry for this purpose instead and you insert items names similarly to the the other Tkinter.Entry()
entries.
The insertion method is this one:
def insert_data(self): """ Insertion method. """ self.treeview.insert('', 'end', text="Item_"+str(self.i), values=(self.dose_entry.get() + " mg", self.modified_entry.get())) # Increment counter self.i = self.i + 1
May be the main trick here is to retrieve the data the user types using get()
method which thing is represented by self.dose_entry.get()
and self.dose_modified.get()
actions.
This is done, you need now to bind this method to the button to be pressed to trigger the insertion action using the command
option:
self.submit_button = Tkinter.Button(self.parent, text="Insert", command=self.insert_data)
Full program:
'''Created on Mar 21, 2016@author: Bill Begueradj'''try: import Tkinter import ttkexcept ImportError: # Python 3 import tkinter as Tkinter import tkinter.ttk as ttkclass Begueradj(Tkinter.Frame): ''' classdocs ''' def __init__(self, parent): ''' Constructor ''' Tkinter.Frame.__init__(self, parent) self.parent=parent self.initialize_user_interface() def initialize_user_interface(self): """Draw a user interface allowing the user to type items and insert them into the treeview """ self.parent.title("Canvas Test") self.parent.grid_rowconfigure(0, weight=1) self.parent.grid_columnconfigure(0, weight=1) self.parent.config(background="lavender") # Define the different GUI widgets self.dose_label = Tkinter.Label(self.parent, text="Dose:") self.dose_entry = Tkinter.Entry(self.parent) self.dose_label.grid(row=0, column=0, sticky=Tkinter.W) self.dose_entry.grid(row=0, column=1) self.modified_label = Tkinter.Label(self.parent, text="Date Modified:") self.modified_entry = Tkinter.Entry(self.parent) self.modified_label.grid(row=1, column=0, sticky=Tkinter.W) self.modified_entry.grid(row=1, column=1) self.submit_button = Tkinter.Button(self.parent, text="Insert", command=self.insert_data) self.submit_button.grid(row=2, column=1, sticky=Tkinter.W) self.exit_button = Tkinter.Button(self.parent, text="Exit", command=self.parent.quit) self.exit_button.grid(row=0, column=3) # Set the treeview self.tree = ttk.Treeview(self.parent, columns=('Dose', 'Modification date')) self.tree.heading('#0', text='Item') self.tree.heading('#1', text='Dose') self.tree.heading('#2', text='Modification Date') self.tree.column('#1', stretch=Tkinter.YES) self.tree.column('#2', stretch=Tkinter.YES) self.tree.column('#0', stretch=Tkinter.YES) self.tree.grid(row=4, columnspan=4, sticky='nsew') self.treeview = self.tree # Initialize the counter self.i = 0 def insert_data(self): """ Insertion method. """ self.treeview.insert('', 'end', text="Item_"+str(self.i), values=(self.dose_entry.get() + " mg", self.modified_entry.get())) # Increment counter self.i = self.i + 1def main(): root=Tkinter.Tk() d=Begueradj(root) root.mainloop()if __name__=="__main__": main()
Note:
I coded this in Python 2.7, but it should work if you're using Python 3.x.