# Adapted from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965 import Tkinter class DeleteButton(Tkinter.Button): def __init__(self, parent): self.m_parent = parent Tkinter.Button.__init__(self, parent, text="-", command=self.deleteRow) def deleteRow(self): # print "grid_info:", self.grid_info() # row = self.grid_info()["row"] print "deleting row:", row l = list( self.m_parent.grid_slaves(row=row) ) for w in l: w.grid_forget() class TestDisplayWithGrid: def __init__(self, parent, debug=0,): self.m_debug = debug self.m_parent = parent self.m_currentRow = 0 initialList = [ ("Row1Col1", "Row1Col2", ), ("Row2Col1", "Row2Col2", ), ] # # Widgets to add a row self.m_currentRow = 0 wid = Tkinter.Button( parent, text="+", command=self.addRow, ) wid.grid(row=self.m_currentRow, column=0) wid = Tkinter.Entry( parent, name='col1', text="col1",) wid.grid(row=self.m_currentRow, column=1) if self.m_debug > 0: print "col1:", wid.__dict__ self.m_col1Entry = wid wid = Tkinter.Entry( parent, name='col2', text="col2",) wid.grid(row=self.m_currentRow, column=2) self.m_col2Entry = wid # # Initial rows: for i in initialList: self.m_currentRow += 1 if self.m_debug > 0: print "Adding row:", i wid = DeleteButton( self.m_parent, ) wid.grid(row=self.m_currentRow, column=0,) wid = Tkinter.Label( parent, text=i[0],) wid.grid(row=self.m_currentRow, column=1,) wid = Tkinter.Label( parent, text=i[1],) wid.grid(row=self.m_currentRow, column=2,) # def addRow(self): if self.m_debug > 0: print "clicked button to add row" self.m_currentRow += 1 wid = DeleteButton( self.m_parent, ) wid.grid(row=self.m_currentRow, column=0,) wid = Tkinter.Label( self.m_parent, text=self.m_col1Entry.get(),) wid.grid(row=self.m_currentRow, column=1,) wid = Tkinter.Label( self.m_parent, text=self.m_col2Entry.get(),) wid.grid(row=self.m_currentRow, column=2,) # def mainloop(self): self.m_parent.mainloop() if __name__ == '__main__': root = Tkinter.Tk() root.title("Test dynamic") app = TestDisplayWithGrid(root, debug=10) app.mainloop()