wx.GenericDirCtrl - how to capture events - part two ...

Hi !

I don't understand, wxPython why don't push the events to the inherited objects.
Delphi is better in this, because if I want, I can do 3 things:
1. Override the old method, do my things only.
2. Override the old method, call the parent's method, and do my things.
3. Override the old method, do my things, and then call the parent's method.

That is very flexible, because I can avoid some effects, I can control the object fully.

But in wx I see that many objects drop the events, and the inherited object cannot use/receive them. See: "Grid - Select only one cell".

The very bad solution this for make selection:

#Boa:Dialog:dlg_choosepath

import wx

def create(parent):
     return dlg_choosepath(parent)

[wxID_DLG_CHOOSEPATH, wxID_DLG_CHOOSEPATHBUTTON1, wxID_DLG_CHOOSEPATHBUTTON2,
  wxID_DLG_CHOOSEPATHGDC_MAIN, wxID_DLG_CHOOSEPATHPANEL1,
  wxID_DLG_CHOOSEPATHPANEL2, wxID_DLG_CHOOSEPATHTC_PATH,
] = [wx.NewId() for _init_ctrls in range(7)]

[wxID_DLG_CHOOSEPATHTIM_CHANGING] = [wx.NewId() for _init_utils in range(1)]

class dlg_choosepath(wx.Dialog):
     def _init_coll_boxSizer3_Items(self, parent):
         # generated method, don't edit

         parent.AddWindow(self.button1, 0, border=4, flag=wx.ALL)
         parent.AddWindow(self.button2, 0, border=4, flag=wx.ALL)

     def _init_coll_boxSizer1_Items(self, parent):
         # generated method, don't edit

         parent.AddWindow(self.panel1, 1, border=0, flag=wx.EXPAND | wx.ALL)
         parent.AddWindow(self.panel2, 0, border=0, flag=wx.EXPAND | wx.ALL)

     def _init_coll_boxSizer2_Items(self, parent):
         # generated method, don't edit

         parent.AddWindow(self.gdc_main, 1, border=4, flag=wx.EXPAND | wx.ALL)
         parent.AddWindow(self.tc_path, 0, border=4, flag=wx.EXPAND | wx.ALL)

     def _init_utils(self):
         # generated method, don't edit
         self.tim_changing = wx.Timer(id=wxID_DLG_CHOOSEPATHTIM_CHANGING,
               owner=self)
         self.Bind(wx.EVT_TIMER, self.OnTim_changingTimer,
               id=wxID_DLG_CHOOSEPATHTIM_CHANGING)

     def _init_sizers(self):
         # generated method, don't edit
         self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

         self.boxSizer2 = wx.BoxSizer(orient=wx.VERTICAL)

         self.boxSizer3 = wx.BoxSizer(orient=wx.HORIZONTAL)

         self._init_coll_boxSizer1_Items(self.boxSizer1)
         self._init_coll_boxSizer2_Items(self.boxSizer2)
         self._init_coll_boxSizer3_Items(self.boxSizer3)

         self.SetSizer(self.boxSizer1)
         self.panel1.SetSizer(self.boxSizer2)
         self.panel2.SetSizer(self.boxSizer3)

     def _init_ctrls(self, prnt):
         # generated method, don't edit
         wx.Dialog.__init__(self, id=wxID_DLG_CHOOSEPATH, name=u'dlg_choosepath',
               parent=prnt, pos=wx.Point(445, 223), size=wx.Size(411, 352),
               style=wx.DEFAULT_DIALOG_STYLE, title=u'Choose path')
         self._init_utils()
         self.SetClientSize(wx.Size(403, 318))
         self.Center(wx.BOTH)

         self.panel1 = wx.Panel(id=wxID_DLG_CHOOSEPATHPANEL1, name='panel1',
               parent=self, pos=wx.Point(0, 0), size=wx.Size(403, 287),
               style=wx.TAB_TRAVERSAL)

         self.panel2 = wx.Panel(id=wxID_DLG_CHOOSEPATHPANEL2, name='panel2',
               parent=self, pos=wx.Point(0, 287), size=wx.Size(403, 31),
               style=wx.TAB_TRAVERSAL)

         self.button1 = wx.Button(id=wxID_DLG_CHOOSEPATHBUTTON1, label=u'Ok',
               name='button1', parent=self.panel2, pos=wx.Point(4, 4),
               size=wx.Size(75, 23), style=0)
         self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
               id=wxID_DLG_CHOOSEPATHBUTTON1)

         self.button2 = wx.Button(id=wxID_DLG_CHOOSEPATHBUTTON2, label=u'Cancel',
               name='button2', parent=self.panel2, pos=wx.Point(87, 4),
               size=wx.Size(75, 23), style=0)
         self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button,
               id=wxID_DLG_CHOOSEPATHBUTTON2)

         self.gdc_main = wx.GenericDirCtrl(defaultFilter=0, dir='.', filter='',
               id=wxID_DLG_CHOOSEPATHGDC_MAIN, name=u'gdc_main',
               parent=self.panel1, pos=wx.Point(4, 4), size=wx.Size(395, 250),
               style=wx.DIRCTRL_3D_INTERNAL | wx.SUNKEN_BORDER)
         self.gdc_main.SetLabel(u'gdc_main')
         self.gdc_main.Bind(wx.EVT_LEFT_DCLICK, self.OnGdc_mainLeftDclick)
         self.gdc_main.Bind(wx.EVT_LEFT_UP, self.OnGdc_mainLeftUp)
         self.gdc_main.Bind(wx.EVT_LEFT_DOWN, self.OnGdc_mainLeftDown)
         self.gdc_main.Bind(wx.EVT_KEY_UP, self.OnGdc_mainKeyUp)
         self.gdc_main.Bind(wx.EVT_KEY_DOWN, self.OnGdc_mainKeyDown)
         self.gdc_main.Bind(wx.EVT_CHAR, self.OnGdc_mainChar)
         self.gdc_main.Bind(wx.EVT_PAINT, self.OnGdc_mainPaint)

         self.tc_path = wx.TextCtrl(id=wxID_DLG_CHOOSEPATHTC_PATH,
               name=u'tc_path', parent=self.panel1, pos=wx.Point(4, 262),
               size=wx.Size(395, 21), style=wx.TE_READONLY, value='textCtrl1')

         self._init_sizers()

     def __init__(self, parent):
         self._init_ctrls(parent)
         self.Path=None
         self.tim_changing.Start(10,wx.TIMER_CONTINUOUS)

     def OnButton1Button(self, event):
         self.OnPathChanged()
         self.Path=self.tc_path.GetValue()
         self.Close()
         event.Skip()

     def OnButton2Button(self, event):
         self.Close()
         event.Skip()

     def OnPathChanged(self):
         s=self.gdc_main.GetPath()
         s2=self.tc_path.GetValue()
         if s<>s2:
             self.tc_path.SetValue(s)

     def OnGdc_mainLeftDclick(self, event):
         self.OnPathChanged()
         event.Skip()

     def OnGdc_mainLeftUp(self, event):
         self.OnPathChanged()
         event.Skip()

     def OnGdc_mainLeftDown(self, event):
         self.OnPathChanged()
         event.Skip()

     def OnGdc_mainKeyUp(self, event):
         self.OnPathChanged()
         event.Skip()

     def OnGdc_mainKeyDown(self, event):
         self.OnPathChanged()
         event.Skip()

     def OnGdc_mainChar(self, event):
         self.OnPathChanged()
         event.Skip()

     def OnGdc_mainPaint(self, event):
         self.OnPathChanged()
         event.Skip()

     def OnTim_changingTimer(self, event):
         self.OnPathChanged()
         event.Skip()

if __name__ == '__main__':
     app = wx.PySimpleApp()
     wx.InitAllImageHandlers()
     dlg = create(None)
     try:
         dlg.ShowModal()
     finally:
         dlg.Destroy()
     app.MainLoop()

you can do this things in Python too… if you want the old method simply get it from the class and call it with the instance of your current object.

For example id ExtendedClass inherits SimpleClass and SimpleClass has a SimpleMethod that you want to overide bu still want to call the old one you can do it like this:

class ExtendedClass(SimpleClass):
def SimpleMethod(self):

    self.NewAndSmartMethods()# your new stuff
    SimpleClass.SimpleMethod(self) # execute the old code
···

On 2/15/06, Durumdara durumdara@gmail.com wrote:

Hi !

I don’t understand, wxPython why don’t push the events to the inherited
objects.
Delphi is better in this, because if I want, I can do 3 things:

  1. Override the old method, do my things only.

  2. Override the old method, call the parent’s method, and do my things.

  3. Override the old method, do my things, and then call the parent’s method.

Events are pushed to subclasses, just not in the way that you expect.
In base wxPython, events are bound to specific methods via one of the
following:
    object.Bind(EVT_EVENT, object.method, source)
    object.Bind(EVT_EVENT, object.method)
    EVT_EVENT(object, source, object.method)
    EVT_EVENT(object, object.method)

This does 'early binding' to the method in the class. That means that
if you want to override event handling, you are going to need to bind
the event you want to capture. Once that event is fired, you can Skip
() the event so that it is handled by the underlying implementation
after your handler is finished (or use wx.CallAfter() to schedule a
different handler for the event), or not use Skip().

I would also mention that you would likely learn and understand the
combination of Python and wxPython better if you weren't using Boa
Constructor.

- Josiah

···

Durumdara <durumdara@gmail.com> wrote:

Hi !

I don't understand, wxPython why don't push the events to the inherited
objects.