[wxPython] easy use classes

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[wxPython] easy use classes

Robin Becker-7
I'm a long time Tk user and would like to get into wxPython.

Are there any packaged widgets which don't require a huge effort to produce something useful?
The current stuff is almost unusable to an idiot like me. There appear to be a huge number of
options/constants etc which make life extremely confusing.

As an example I hacked this up to represent a simple message box class.
To ask a question you would do

qb=MessageBox(style='question yesno')
ans=qb.show('Want to continue')
if ans=='yes':

from wxPython.wx import wxApp, wxMessageDialog, wxDefaultPosition, NULL, \
                        wxOK, wxCANCEL, wxYES_NO, wxYES, wxNO, wxNO_DEFAULT, \
                        wxCENTRE, wxICON_HAND, \
                        wxICON_QUESTION, wxICON_INFORMATION, \
                        wxID_OK, wxID_CANCEL, wxID_YES, wxID_NO

from string import upper, find

class _App(wxApp):
    def OnInit(self):
        return 1

    def __del__(self):

def _GetApp():
        return _App(0)
        return None

class MessageBox:
                'wxOK': wxOK,
                'wxCANCEL': wxCANCEL,
                'wxYES_NO': wxYES_NO,
                'wxNO': wxNO,
                'wxYES': wxYES,
                'wxNO_DEFAULT': wxNO_DEFAULT,
                'wxCENTRE': wxCENTRE,
                'wxICON_HAND': wxICON_HAND,
                'wxICON_QUESTION': wxICON_QUESTION,
                'OK': wxOK,
                'CANCEL': wxCANCEL,
                'YES_NO': wxYES_NO,
                'YESNO': wxYES_NO,
                'NO': wxNO,
                'YES': wxYES,
                'NO_DEFAULT': wxNO_DEFAULT,
                'CENTRE': wxCENTRE,
                'ICON_HAND': wxICON_HAND,
                'ICON_QUESTION': wxICON_QUESTION,
                'HAND': wxICON_HAND,
                'QUESTION': wxICON_QUESTION,

    retval = {wxID_OK: 'ok', wxID_CANCEL: 'cancel', wxID_YES: 'yes', wxID_NO: 'no'}

    def __init__(self,caption='',pos=wxDefaultPosition,style=wxOK | wxICON_INFORMATION):
        self.caption = caption
        self.pos = pos
        if type(style)==type(''):
            s = upper(style)
                style = 0
                for i in self.style_info.keys():
                    if find(s,i)>=0:
                        style = style | self.style_info[i]

        self.style = style

    def show(self,msg):
        app = _GetApp()
        dlg = wxMessageDialog(NULL, msg, self.caption,self.style,self.pos)
        val = dlg.ShowModal()
        return self.retval[val]
Robin Becker

wxPython-users maillist  -  [hidden email]