wxpython event question

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

Following is by no means an elegant solution, but it's the first one that pops into my mind...

shuoyang wrote:

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,....]

itemMapping = # MyList[i] is represented by control with ID itemMapping[i]

#then I have loop:
for v in MyList:
make static text with v
make a button to delete v from MyList, call OnClick

itemMapping.append( (button,text) )

def OnClick(self,event)
need to know the index of v to delete v from MyList, but how?

for i, (object,(button,text)) in enumerate( zip(MyList, itemMapping)):
    if button == event.GetEventObject(): # might be GetId(), been a while :slight_smile:
       del MyList[i]
       # remove controls from parent here
       del itemMapping[i]
       break # all further indices are messed up anyway...

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

In real-world code I'd likely make the model-view split a little more elegant and less fragile, but then I'm somewhat obsessive about those kinds of things.

HTH,
Mike

···

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/

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( )
        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