Newbie question about events and event handling

Patrick J. Anderson pat.j.anderson at gmail.com
Fri Nov 2 14:50:14 PDT 2007


On Fri, 02 Nov 2007 14:16:14 -0700, Christopher Barker wrote:

> Patrick J. Anderson wrote:
>> I used the reference to DayPlanner (option1) in my MiniCalendar and I
>> call a method setDate() on planner instance, but how do I update the
>> widget, so it reflects the change?
> 
> You'll need to post code showing what the DayPlanner class is/does, and
> how. I see this form your previous post, but I don't see what you do
> with the self.date attribute:
> 
>  >> 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

Below is the DayPlanner and MiniCalendar classes. 

At this point I'd like to change the date of the day planner when I 
change the date in the min calendar. I haven't added any data yet, since 
I've been trying to understand how events work in desktop programming 
with wxPython.

I can set the date and print it from withing DayPlanner instance, but I 
can't change the UI (the header of the planner where I use that date).

Do I need to somehow 'refresh'?



# MiniCalendar.py

class MiniCalendar(cal.CalendarCtrl):

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

    def OnCalSelChanged(self, event):
        cal = event.GetEventObject()
        date = cal.GetDate()
        print 'Calendar changed the date to: %s' % date
        dplanner = wx.FindWindowByName('DayPlanner')
        dplanner.setDate(date)
        event.Skip()



# DayPlanner.py

import wx
from wx.lib.scrolledpanel import ScrolledPanel

class DayPlannerPanel(ScrolledPanel):

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

        self.date = wx.GetApp().date

        self.SetName('DayPlanner')
        self.SetBackgroundColour('#000')

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

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

        self._createHeaders()
        
        for h in range(1, 12):
            self._createHourRow(h)
        for h in range(12, 24):
            self._createHourRow(h)

        self.SetAutoLayout(True)
        self.SetSizer(self.fbox)
        self.SetupScrolling()


    def _createHeaders(self):

        hp1 = wx.Panel(self, wx.NewId())
        text1 = wx.StaticText(hp1, wx.NewId(), 'Hours', [0.0], [50, -1],)
        hbox1 = wx.BoxSizer()
        hbox1.Add(text1, 1, wx.ALL | wx.EXPAND, 0)
        hp1.SetSizer(hbox1)
        self.fbox.Add(hp1, 1, wx.EXPAND)

        hp2 = wx.Panel(self, wx.NewId())
        hbox2 = wx.BoxSizer()
        text2 = wx.StaticText(hp2, wx.NewId(), self.date.strftime('%A %d %
B, %Y %H:%M:%S'), 
            style = wx.ALIGN_CENTER)
        hbox2.Add(text2, 1, wx.ALL | wx.EXPAND, 5)
        hp2.SetSizer(hbox2)
        self.fbox.Add(hp2, 1, wx.EXPAND)


    def _createHourRow(self, hour):

        if hour <= 11:
            x = 'am'
        else:
            x = 'pm'

        l_panel = wx.Panel(self, wx.NewId())
        l_box = wx.BoxSizer(wx.VERTICAL)
        h_label = wx.StaticText(l_panel, wx.NewId(), '%i %s' % (hour, x), 
            style = wx.ALIGN_RIGHT)
            
        l_box.Add(h_label, 1, wx.ALL | wx.EXPAND, 5)
        l_panel.SetSizer(l_box)
        self.fbox.Add(l_panel, 1, wx.ALL | wx.EXPAND, 0)

        h_panel = wx.Panel(self, wx.NewId())
        h_box = wx.BoxSizer(wx.VERTICAL)
        h_panel.SetSizer(h_box)
        self.fbox.Add(h_panel, 1, wx.ALL | wx.EXPAND, 0)


    def getDate(self):
        return self.date


    def setDate(self, d):
        self.date = d
        print 'New planner date has been set: %s' % d





More information about the wxpython-users mailing list