[wxPython-users] Default wx.Choice() Value?

Christopher Barker Chris.Barker at noaa.gov
Fri Dec 22 09:25:17 PST 2006


Rich Shepard wrote:
> On Thu, 21 Dec 2006, Christopher Barker wrote:
>> def _setVarSource(self, s):
>>    self.vsource.SetStringSelection(s)
>>
>> def _getVarSource(self):
>>    return self.vsource.GetStringSelection()
>>
>> self.varSource = property(_getVarSource, _setVarSource)

> This module was originally written last summer when I was just 
> starting on
> this application and python/wxPython. Now I'm revising it so it works. 
> There
> are 15 widgets on this notebook page: wx.TextCtrl(), wx.Choice, 
> wx.SpinCtrl,
> and a wx.RadioBox. Most of the time the default values of the latter three
> types are what are needed. But, if different values are selected then they
> need to be displayed and written to the database when the Save button is
> pressed.

The above code should support that just fine. IIRC, you're keeping the 
GUI and database manipulation code separate. In that case, when "Save" 
is pressed, the DB code needs to query the GUI for the current values. 
THE above support that, and conforms to a few principles:

* Only keep the same data in one place -- so let the control store the 
current value, no need for a "shadow" attribute in the GUI class.

* The law of Demeter -- the DB class shouldn't need to know that a given 
value is kept in a wx.Choice, only that the GUI class has an attribute 
that holds the data.

* "Getters and setters are evil. Evil, evil, I say!"[1] OK, maybe not 
evil, but I do think properties are an elegant way to write and use 
code, and, in particular, let you write code in the simplest way 
possible, then re-factor easily later if need be.

One possible change is to have your GUI class have a method (or 
property) that returns a dictionary of all the values you need, rather 
than having the DB class query it for each one individually.

-Chris


[1] Phillip J. Eby: "Python is not Java": 
http://dirtsimple.org/2004/12/python-is-not-java.html




-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list