Binding events for a PopUp menu

hello…

I have a doubt with the popup menu demo in the wxpython demo..

the events of the popup menu are binded inside the method that

creates the popup menu…

        def OnContextMenu(self, event):
self.log.WriteText("OnContextMenu\n")
# only do this part the first time so the events are only bound once

<details class='elided'>
<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>

#
# Yet another anternate way to do IDs. Some prefer them up top to
# avoid clutter, some prefer them close to the object of interest
# for clarity. if not hasattr(self, "popupID1"):
self.popupID1 = wx.NewId()
self.popupID2 = wx.NewId()
self.popupID3 = wx.NewId()
self.popupID4 = wx.NewId()
self.popupID5 = wx.NewId()
self.popupID6 = wx.NewId()
self.popupID7 = wx.NewId()
self.popupID8 = wx.NewId()
self.popupID9 = wx.NewId()
self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
self.Bind(wx.EVT_MENU, self.OnPopupFour, id=self.popupID4)
self.Bind(wx.EVT_MENU, self.OnPopupFive, id=self.popupID5)
self.Bind(wx.EVT_MENU, self.OnPopupSix, id=self.popupID6)
self.Bind(wx.EVT_MENU, self.OnPopupSeven, id=self.popupID7)
self.Bind(wx.EVT_MENU, self.OnPopupEight, id=self.popupID8)
self.Bind(wx.EVT_MENU, self.OnPopupNine, id=self.popupID9)
why is this done there and not in the main class __init__ method? is it just a more aesthetic way of binding the events or it is preferred that way for performance or something?

Hi,

it doesn't matter where you bind the events, in this case it's the
only way to bind the events, because you have the IDs just in this
method.
Anyway this Code doesnt look pretty, I would do it like this:

for popupid in xrange(10): #ofc you can also use wx.NewId() in a LC
    self.Bind(wx.EVT_MENU, lambda evt: self.on_popup(evt,
popup_id=popupid), id=popupid) # you can also use functools.partial
instead of lambda

then you can chose what you want to do in "on_popup" (e.g. use a dict
to seperate the popups, my_popups[popup_id])

Advantage of my method, you have just one callback function and you're
code gets nicer, shorter and easier to read.

···

On 9 Apr., 03:27, PythonJourney <pythonjour...@gmail.com> wrote:

hello..

I have a doubt with the popup menu demo in the wxpython demo..

the events of the popup menu are binded inside the method that creates
the popup menu..

 def OnContextMenu\(self, event\):
     self\.log\.WriteText\(&quot;OnContextMenu\\n&quot;\)

     \# only do this part the first time so the events are only bound once
     \#
     \# Yet another anternate way to do IDs\. Some prefer them up top to
     \# avoid clutter, some prefer them close to the object of interest
     \# for clarity\.
     if not hasattr\(self, &quot;popupID1&quot;\):
         self\.popupID1 = wx\.NewId\(\)
         self\.popupID2 = wx\.NewId\(\)
         self\.popupID3 = wx\.NewId\(\)
         self\.popupID4 = wx\.NewId\(\)
         self\.popupID5 = wx\.NewId\(\)
         self\.popupID6 = wx\.NewId\(\)
         self\.popupID7 = wx\.NewId\(\)
         self\.popupID8 = wx\.NewId\(\)
         self\.popupID9 = wx\.NewId\(\)

         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupOne, id=self\.popupID1\)
         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupTwo, id=self\.popupID2\)
         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupThree, id=self\.popupID3\)
         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupFour, id=self\.popupID4\)
         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupFive, id=self\.popupID5\)
         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupSix, id=self\.popupID6\)
         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupSeven, id=self\.popupID7\)
         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupEight, id=self\.popupID8\)
         self\.Bind\(wx\.EVT\_MENU, self\.OnPopupNine, id=self\.popupID9\)

why is this done there and not in the main class __init__ method? is it just a more aesthetic way of binding the events or it is preferred that way for performance or something?