A way, how I code dialog boxes under my win platform
Variant a)
- "std" dialog
- do not forget to destroy the dialog box
- dialog box info is lost between calls
class MyDialog1(wxDialog):
def __init__(self, parent, id):
wxDialog.__init__(self, parent, id, "MyDialog", wxPoint(50,50), wxSize(300, 200))
self.CentreOnParent()
self.bu1 = wxButton(self, 1001, "&OK", wxPoint(100, 140), wxDefaultSize)
self.bu2 = wxButton(self, 1002, "&Cancel", wxPoint(200, 140), wxDefaultSize)
self.txt1 = wxTextCtrl(self, 2001, "bla bla bla...", wxPoint(20, 20), wxSize(150, 20))
EVT_BUTTON(self, 1001, self.OnClick1)
EVT_BUTTON(self, 1002, self.OnClick2)
EVT_CLOSE(self, self.OnClick2)
EVT_CHAR_HOOK(self, self.OnCharHook)
#button ok or Alt+o, hide dlg, return wxID_OK, dlg not destroyed
def OnClick1(self, event):
self.EndModal(wxID_OK)
#button cancel or Alt+o, hide dlg, return wxID_CANCEL, dlg not destroyed
def OnClick2(self, event):
self.EndModal(wxID_CANCEL)
#button [X], hide dlg, return wxID_CANCEL, dlg not destroyed
def OnCloseWindow(self, event):
self.EndModal(wxID_CANCEL)
#esc key, hide dlg, return wxID_CANCEL, dlg not destroyed
def OnCharHook(self, event):
if event.KeyCode() == WXK_ESCAPE:
self.EndModal(wxID_CANCEL)
else:
event.Skip()
Variant b)
- extended a) version
- "auto destroying" dialog box
- the contain of the dialog box is saved between dialog calls
class MySuperDialog1(wxDialog):
bufval = 'bla bla bla'
retval = wxID_CANCEL
def __init__(self, parent, id):
self.dlg = MyDialog1(parent, id)
self.dlg.txt1.SetValue(MySuperDialog1.bufval)
ret = self.dlg.ShowModal()
if ret == wxID_CANCEL:
MySuperDialog1.retval = wxID_CANCEL
self.dlg.Destroy()
else: #wxID_OK:
MySuperDialog1.bufval = self.dlg.txt1.GetValue()
MySuperDialog1.retval = wxID_OK
self.dlg.Destroy()
In the caller:
dlg = MySuperDialog1(self, -1)
print dlg.retval
print dlg.bufval
Jean-Michel Fauth, Switzerland