Jim Kleckner wrote:
[snip]
What I actually want is to construct and add new widgets
during the mainloop().  So a button that appends new labels
at the bottom based on a text entry field would demonstrate
the issue.

I've been playing with this extremely simple and interesting
tVector helper code from here:
  http://www.freenet.org.nz/python/tVector/

Ideally, I just want to reach in and extend the widget list.
I'm attaching a file with some concept code using tVector
(yes, I should rewrite it with grid, I guess).

The grid manager looks interesting and it seems that it
ought to be able do something like this but the methods
don't obviously have an append() or some such.

Ok, I hacked together a grid-based demo.
Care to comment on this code?

Jim
# 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()
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to