[wxPython-users] XML subclassing and wx.lib.masked.TimeCtrl

Robin Dunn robin at alldunn.com
Mon Sep 11 12:58:42 PDT 2006


Basil Shubin wrote:
> Robin Dunn wrote:
>> Basil Shubin wrote:
>>> Robin Dunn wrote:
>>>> Basil Shubin wrote:
>>>>> Robin Dunn wrote:
>>>>>> Basil Shubin wrote:
>>>>>>
>>>>>>>
>>>>>>> # get masked controls start/finish time
>>>>>>> self.startTime = xrc.XRCCTRL(self.parent, 'startTime')
>>>>>>> self.finishTime = xrc.XRCCTRL(self.parent, 'finishTime')
>>>>>>>
>>>>
>>>>> For the above example I make following changes:
>>>>>
>>>>> self.parent.Bind(wx.lib.masked.EVT_TIMEUPDATE,
>>>>>                  self.OnTimeUpdate,
>>>>>                  self.startTime.timeCtrl)
>>>>>
>>>>> and I recive an error:
>>>>>
>>>>> AttributeError: 'WorkoutTime' object has no attribute 'timeCtrl'
>>>>>
>>>>> What is my fault?
>>>>>
>>>>
>>>> You probably want to use just self.startTime, don't you?  Are you 
>>>> expecting self.startTime to have a timeCtrl attribute?  Do you give 
>>>> it one in your code anyplace?
>>>
>>> I am puzzled totally! Yes, I want to use just self.startTime. 
>>> timeCtrl is created in the OnCreate method of WorkoutTime subclass, 
>>> is it timeCtrl its attribute or what? self.startTime itself is not 
>>> wxTimeCtrl, right? so it cannot recive EVT_TIMEUPDATE event, right? 
>>> Uhh, I am totally puzzled! :-(
>>
>> I think I am confused now too.
>>
>> What type is self.startTime?  I assume that is the object you are 
>> creating with your XRC subclass?
>>
>> If it is not a TimeCtrl, but contains one, then yes, you'll need to do 
>> something like self.startTime.timeCtrl to get to it, but if 
>> self.startTime.timeCtrl is not created until the OnCreate then it may 
>> not exist yet.  One approach you can take is to move the event handler 
>> to the WorkoutTime class and do the binding in OnCreate.  Then the 
>> handler can call a method in the parent if needed, or perhaps just 
>> send a message, or whatever.
> 
> Doh! I am still confused!
> 
> Can somebody give me explanation how I can get value from self.time.Ctrl 
>  that from WorkoutTime in other class - WorkoutsTab. Please, give me 
> explanation on a example code I provide. Is it what I do is right? Maybe 
> I should done this task in other way?
> 

> 
> class WorkoutTime(wx.Panel):
>     """Masked control for workout start/finish time"""
> 
>     def __init__(self):
>         p = wx.PrePanel()
>         # the Create step is done by XRC.
>         self.PostCreate(p)
>         self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate)
> 
>     def OnCreate(self, event):
>         """Create masked control for time control"""
>         self.timeCtrl = wx.lib.masked.TimeCtrl(self, fmt24hr=True,
>                                                display_seconds=False)
>         sizer = wx.BoxSizer(wx.VERTICAL)
>         sizer.Add(self.timeCtrl)
>         self.SetSizerAndFit(sizer)
>         self.GetParent().Layout()
> 
>         self.Bind(wx.lib.masked.EVT_TIMEUPDATE,
>                   self.OnTimeUpdate,
>                   self.timeCtrl)
> 
>     def OnTimeUpdate(self, event):
>         return self.timeCtrl.GetValue()


The return value (if any) of an event handler is ignored.  In most cases 
there is no place to put the value since the calling of the handler is 
not coming from your code.  What you probably want is something like this:

	def GetValue(self):
		return self.timeCtrl.GetValue()

If you are running into problems with the timeCtrl not being created yet 
(because the widget has not yet been realized on the display) then you 
can protect yourself something like this:

	def GetValue(self):
		if hasattr(self, 'timeCtrl'):
			return self.timeCtrl.GetValue()
		else:
			return ""


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list