Newbie question about events and event handling

Patrick J. Anderson pat.j.anderson at gmail.com
Wed Oct 31 16:39:27 PDT 2007


Hi, my background is in python web programming and I'm new to desktop 
programming and wxPython.

My question is about events and event handling, which I still don't quite 
understand, as it seems rather complex. 

I have a Splitter Window and on the left side I have a CalendarCtrl and 
the right side I have a day planner.

By changing the selected date in the CalendarCtrl I'd like to change the 
date for the planner on the right.

How can I do this? How can I send the date to the instance of DayPlanner 
and change its view to reflect the new date?

Below is a sample code to illustrate my problem:


class PlannerPanel(wx.SplitterWindow):
    def __init__(self, *args, **kwds):
        wx.SplitterWindow.__init__(self, *args, **kwds)
        
        hbox = wx.BoxSizer(wx.HORIZONTAL)

        self.left_pane = LeftPane(self, wx.NewId())
        self.right_pane = RightPane(self, wx.NewId())
        
        self.SplitVertically(self.left_pane, self.right_pane)
        self.SetMinimumPaneSize(300)
        
        hbox.Add(self, 1, wx.ALL|wx.EXPAND, 0)
        self.SetSizer(hbox)
        self.Layout()


class LeftPane(wx.Panel):

    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)

        vbox = wx.BoxSizer(wx.VERTICAL)
        calendar = MiniCalendar(self, wx.NewId(), wx.DateTime_Now())
        vbox.Add(calendar, 0, wx.ALL | wx.GROW, 1) 
        
        self.SetSizer(vbox)
        self.Layout()


class MiniCalendar(cal.CalendarCtrl):

    def __init__(self, *args, **kwargs):
        cal.CalendarCtrl.__init__(self, *args, **kwargs)

        self.Bind(cal.EVT_CALENDAR_SEL_CHANGED, 
            self.OnCalSelChanged, id = self.GetId())

    def OnCalSelChanged(self, event):
        cal = event.GetEventObject()
        w = wx.FindWindowByName('Day Planner')
        w.setDate(cal.GetDate())
        # print cal.GetDate()


class RightPane(wx.Panel):

    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)
        
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        
        nb = wx.Notebook(self, wx.NewId(), 
                style = wx.NB_BOTTOM | 
                wx.TAB_TRAVERSAL |
                wx.SUNKEN_BORDER)
        
        nb_pane_day         = DayPlanner(nb, wx.NewId())        
        nb.AddPage(nb_pane_day, 'Day')
        
        hbox.Add(nb, 1, wx.ALL | wx.GROW, 1)       
        
        self.SetSizer(hbox)
        self.Layout()


class DayPlanner(ScrolledPanel):

    def __init__(self, *args, **kwargs):
        ScrolledPanel.__init__(self, *args, **kwargs)

        self.date = datetime.today()
        
	self.SetName('Day Planner')

        self.fbox = wx.FlexGridSizer(0, 2, 1, 1)
        self.fbox.AddGrowableCol(1)

        for i in range(1,24):
            self.fbox.AddGrowableRow(i)

        self._createHeaders()
        self._createRows(h)

        self.SetSizer(self.fbox)
        self.Layout()
        self.SetupScrolling()

    ...

    def setDate(self, date):
        self.date = date





More information about the wxpython-users mailing list