1

I'm trying to do a class that holds all my message dialogs such I don't need to have them mixed with my logic. The aim is to keep the GUI code away of the Non-GUI stuff, I'd like achieve a nice Model-View-Controller architecture

class errorMessageDialog(wx.MessageDialog):

    def __init__(self, message, caption= 'Error'):
        wx.MessageDialog.__init__(self, None, message, caption, wx.OK | wx.ICON_ERROR)
        self.ShowModal()
        self.Destroy()

I've manage to do it for simple error messages (see exemple above), however I'd like to do a message dialog where the user can select either OK or Cancel. In that case the method should return a boolean. I tried the following:

class safetyCheck(wx.MessageDialog):
    def __init__(self, message="Make sure the following commands are correct", caption= 'Safety Warning'):
    wx.MessageDialog.__init__(self, None, message, caption, wx.OK | wx.CANCEL | wx.CENTRE | wx.ICON_EXCLAMATION)

        if self.ShowModal() == wx.ID_OK:
            safetyCheckPassed = True
        else:
            safetyCheckPassed = False
        return safetyCheckPassed
        self.Destroy()

I might be mistaken but I think what I'm trying to achieve is not possible cause the self.Destroy method must be called before the return, therefore the return line is never run.

Obviously I could create my dialog inside my model code but this would be messy.

EDIT:

I considered the suggestions below but I didn't manage to make any of those work I ended up with this solution.

class SafetyCheckAFT(wx.MessageDialog):

    def __init__(self):
        pass

    def dialog(self):
        message="Make sure the following commands are correct"
        caption= 'Safety Warning'
        dlg= wx.MessageDialog(None, message, caption, wx.OK | wx.CANCEL | wx.CENTRE | wx.ICON_EXCLAMATION)

        if dlg.ShowModal() == wx.ID_OK:
            self.safetyCheckPassed = True
        else:
            self.safetyCheckPassed = False
        dlg.Destroy()

    def isSaftyCheckPassed(self):
         return self.safetyCheckPassed


sc=SafetyCheckAFT()
sc.dialog()
if sc.isSaftyCheckPassed():
    print("cool beans")
user3770060
  • 115
  • 1
  • 7

2 Answers2

1

Take a look at: How do I correctly clean up a Python object?

Using the python with statement in conjunction with an __exit__ statement you should be able to have your resource cleaned up.

You may also want to try use try: and finally: putting the return statement at the end of the try and the cleanup in finally:

I haven't tested this though, I haven't used python in a while.

Community
  • 1
  • 1
ozborn
  • 980
  • 5
  • 24
1

Let me suggest another solution. You could also have a helper class that makes the dialog for you:

Model -> Helper -> safetyCheck

The helper class has a static method which creates the dialog, saves the result, destroys the dialog, and then returns the result.

class safetyCheck(wx.MessageDialog):
    def __init__(self, message="Make sure the following commands are correct", caption= 'Safety Warning'):
        wx.MessageDialog.__init__(self, None, message, caption, wx.OK | wx.CANCEL | wx.CENTRE | wx.ICON_EXCLAMATION)

    def ShowModal(self):
        return wx.MessageDialog.ShowModal() == wx.ID_OK

class Helper:
    @staticmethod
    def safety_check_popup(*args, **kwargs):
        dialog = safetyCheck(*args, **kwargs)
        result = dialog.ShowModal()
        dialog.Destroy()
        return result
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93