The following program shows how to use nested scopes and lambda pseudo-closures to do it:
···
###################################################################################
class TestUI( object ) :
"Model User Interface class"
def __init__( self ) :
self.frame = wx.Frame( None, -1, "Test" )
self.panel = wx.Panel( self.frame, -1 )
num_rows = 4
num_cols = 2
self.sizer = wx.FlexGridSizer( num_rows, num_cols, 5 , 5 )
for row in range( num_rows ) :
label = wx.StaticText( self.panel, -1, str( row ) )
self.sizer.Add( label )
button = wx.Button( self.panel, -1, str( row ) )
self.sizer.Add( button )
wx.EVT_BUTTON( self.panel,
button.GetId( ),
(lambda index : lambda event : self.OnClick( event, index ) ) ( row )
)
self.panel.SetSizer( self.sizer )
self.sizer.SetSizeHints( self.frame )
self.frame.Show( )
return
def OnClick( self, event, idx = -1 ) :
"event handler for button pressed"
wx.LogMessage( "Button id %d pressed, idx = %d" % ( event.GetId( ), idx ) )
return
###################################################################################
class TestApp ( wx.App ) :
def OnInit( self ) :
self.ui = TestUI( )
return True
###################################################################################
def _test( ) :
test_app = TestApp( )
test_app.MainLoop( )
if __name__ == "__main__" :
_test( )
In message <3FECA4D9@webmail.uiuc.edu>, shuoyang <shuoyang@students.uiuc.edu> writes:
Hi dear wxPython users:
I am pretty new to wxPython, and find it amazing, not to mention powerful!
But I've run into one problem, I've read thru the tutorials, but can't seem to
figure out how to do this:
#I have a list of things:
MyList = [elem1,elem2,....]
#then I have loop:
for v in MyList:
make static text with v
make a button to delete v from MyList, call OnClick
def OnClick(self,event)
need to know the index of v to delete v from MyList, but how?
Since python list is not object in wxPython, i don't think the event parameter
in OnClick would help. Can anyone offer a hint? Thanks!
John
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
--
Pythonologist