wxPython and macros

Mel Wilson mwilson at the-wire.com
Sun Oct 26 15:04:57 EST 2003


In article <APGKGBCGOCN4OOOPLYAMCHFYGIOLBQN4NTJUFTOA at ziplip.com>,
mike420 at ziplip.com wrote:
>Tayss wrote:
>
>>
>> app   = wxPySimpleApp()
>> frame = MainWindow(None, -1, "A window")
>> frame.Show(True)
>> app.MainLoop()
>>
>
>Why do you need a macro for that? Why don't you just write

   Is this my fault?  Once upon a time I used (and posted)
code like

    new_control_macro = """ \
        hs = wxBoxSizer (wxHORIZONTAL)
        hs.Add (5, 0, 0)
        hs.Add (wxStaticText (self, -1, %(label)s, wxDefaultPosition), 0)
        self.%(name)s_ctrl = wxTextCtrl (self, -1, "", wxDefaultPosition)
        hs.Add (self.%(name)s_ctrl, 1, wxLEFT | wxRIGHT, 5)
        vs.Add (hs, 0, wxEXPAND)
    """
...
    vs = wxBoxSizer (wxVERTICAL)
    exec new_control_macro % {'label':'First name', 'name':'fname'}
    exec new_control_macro % {'label':'Last name', 'name':'lname'}
    exec new_control_macro % {'label':'Cell Phone', 'name':'cellular'}


   Now that functions can access names in outer lexical
scopes, this code can be improved to

    def new_control (label):
        hs = wxBoxSizer (wxHORIZONTAL)
        hs.Add (5, 0, 0)
        hs.Add (wxStaticText (self, -1, label, wxDefaultPosition), 0)
        ctrl = wxTextCtrl (self, -1, "", wxDefaultPosition)
        hs.Add (ctrl, 1, wxLEFT | wxRIGHT, 5)
        vs.Add (hs, 0, wxEXPAND)
        return ctrl
...
    vs = wxBoxSizer (wxVERTICAL)
    self.fname_ctrl = new_control ("First Name")
    self.lname_ctrl = new_control ("Last Name")
    self.cellular_ctrl = new_control ("Cell Phone")


because the function can do the housekeeping on vs by itself.

(new_control is defined locally within another function,
just as new_control_macro would have been.)


(Passing vs in as a parameter wasn't done because I considered
new_control to be boilerplate code that would be replicated
many times with just the two customizations each time.  `vs`
here stands for all the routine drudgery that might be
required by the general task.)

        Regards.        Mel.




More information about the Python-list mailing list