First of all, thank you all and espeically pythonogolist for the beautiful
solution using lambda, not to mention the cool name (lambda pseudo-closures).
Pythonologist's solution worked perfectly and is the most elegant.
But prior to his solution, I had managed to get it to work like this, taking
advantage of the fact of manually assigning the index of the list to the
button's ID, and then later in OnClick(self, event), extract the button's
ID(and also the index to the value of the list to be removed) from
event.getId().
Like this:
list =
for i,v in enumerate(list):
sizer.add(statictext)
# this button's ID is the index of the element in the list
sizer.add(button(panel, i, "mybutton with index %d" % i))
EVT_BUTTON(self, i, self.OnClick)
def self.OnClick(self,event):
# Since the button's ID is the index we want
del list[event.GetId()]
But, thanks again everyone
wxPython rocks.
John (shuoyang)
···
===== Original Message From Pythonologist <pythonologist@octave.demon.co.uk>
=====
Hi shuoyang,
The following program shows how to use nested scopes and lambda
pseudo-closures to do it:
import wx
#############################################################################
######
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( )
returndef 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 OnClickdef 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---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org