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")