WXWINDOW: CANCEL -button and validation

Pekka Niiranen krissepu at vip.fi
Wed Feb 20 21:55:31 CET 2002


Hi,

In code below:

-  when user enters empty values and presses OK -button, validator
    warns about the empty values.

-  when user enters empty values and presses CANCEL -button, validator
    is not run and I must add "useless checks for empty values" in code
+ default values

- I can kill the wxDialog with empty values on boxes.

Questions:

1) How can I run Validate(self, parent) -method when CANCEL -button is
pressed ?
2) How can disable the close button (X -button on top right corner of
window) from wxDialog ?

 -------------- Code starts here --------------------------------

class EditConversionParameters(wxDialog):
    def __init__(self, parent, conv_fileprefix, conv_fileprefix_default,

                 conv_prefix, conv_prefix_default,
                 conv_suffix, conv_suffix_default, style):
        wxDialog.__init__(self, parent, -1, "  Conversion parameters",
                          wxDefaultPosition, wxSize(280, 150), style)

        # Defaults
        self.conv_fileprefix_default = conv_fileprefix_default
        self.conv_prefix_default = conv_prefix_default
        self.conv_suffix_default = conv_suffix_default

XXXXXXX Useless checks starts here XXXXXXXXXXXXXXXX

        # Make sure user does not leave with empty boxvalues ever !
        if conv_fileprefix == "":
            self.conv_fileprefix = conv_fileprefix_default
        else:
            self.conv_fileprefix = conv_fileprefix

        if conv_prefix == "":
            self.conv_prefix = conv_prefix_default
        else:
            self.conv_prefix = conv_prefix

        if conv_suffix== "":
            self.conv_suffix = conv_suffix_default
        else:
            self.conv_suffix = conv_suffix

XXXXXXX Useless checks stops here XXXXXXXXXXXXXXXX

        # Create validators
        self.conv_fileprefix_validator=InputValidator(ID_FPREF)
        self.conv_prefix_validator=InputValidator(ID_SPREF)
        self.conv_suffix_validator=InputValidator(ID_SSUFF)

        # Add static texts
        self.text1 = wxStaticText(self, -1, "Filename prefix:",
wxPoint(10,24))
        self.text2 = wxStaticText(self, -1, "Conversion prefix:",
wxPoint(10,54))
        self.text3 = wxStaticText(self, -1, "Conversion suffix:",
wxPoint(10,84))

        # Add dialog boxes
        self.box1  = wxTextCtrl(self, -1, self.conv_fileprefix,
wxPoint(100,20),
                                size=(50, -1),
validator=self.conv_fileprefix_validator)
        self.box2  = wxTextCtrl(self, -1, self.conv_prefix,
wxPoint(100,50),
                                size=(50, -1),
validator=self.conv_prefix_validator)
        self.box3  = wxTextCtrl(self, -1, self.conv_suffix,
wxPoint(100,80),
                                size=(50, -1),
validator=self.conv_suffix_validator)

        # Add some buttons
        self.button1 = wxButton(self, ID_CONV_DEFAULT, " Defaults ",
wxPoint(180,20), wxDefaultSize)
        self.button2 = wxButton(self, wxID_OK, " OK ",  wxPoint(180,50),
wxDefaultSize).SetDefault()
        self.button3 = wxButton(self, wxID_CANCEL, " Cancel ",
wxPoint(180,80), wxDefaultSize)

        # Add button events
        EVT_BUTTON(self, ID_CONV_DEFAULT, self.set_defaults)

    def set_defaults(self, event):
        self.box1.SetValue(self.conv_fileprefix_default)
        self.box2.SetValue(self.conv_prefix_default)
        self.box3.SetValue(self.conv_suffix_default)


class InputValidator(wxPyValidator):
    def __init__(self, flag):
        wxPyValidator.__init__(self)
        self.flag = flag
        EVT_CHAR(self, self.OnChar)

    def Clone(self):
        """ All validator classes must implement the Clone function,
        which returns an identical copy of itself."""
        return InputValidator(self.flag)

    def Validate(self, parent):
         """ Validate the contents of the given text control.
         Validator gets executed only when OK -button is pressed"""
         textCtrl = self.GetWindow()
         text = textCtrl.GetValue()
         if len(text) == 0:
             dlg = wxMessageDialog(parent, "All boxes must contain some
text!", " Error", wxOK )
             dlg.ShowModal()
             dlg.Destroy()
             textCtrl.SetFocus()
             return false
         else:
             return true

    def TransferToWindow(self):
         return true # Prevent wxDialog from complaining.

    def TransferFromWindow(self):
         return true # Prevent wxDialog from complaining.

    def OnChar(self, event):
       # Control which characters can be entered to each box
        key = event.KeyCode()
        if key == WXK_BACK or key == WXK_DELETE:
            event.Skip()
            return
        if self.flag == ID_FPREF and (chr(key) in string.ascii_letters
or
                                      chr(key) in string.digits or
                                      chr(key) == "_"):
            event.Skip()
            return
        if self.flag == ID_SPREF and (chr(key) in string.punctuation):
            event.Skip()
            return
        if self.flag == ID_SSUFF and (chr(key) in string.punctuation):
            event.Skip()
            return

        # Returning without calling even.Skip eats the event before it
        # gets to the text control : no text is entered into box.
        return

-pekka-




More information about the Python-list mailing list