I’m working on a program that is going to be used with a Time Motion study. There are times that the user may have to create add new objects (Observer, Subject, Clinic). In order to accomplish this I have employed the use of a modal dialog box, (since all data is associated to a subject, observer, and clinic).
I have created a general class for the dialog boxes, and then just create an instance of this class for each dialog box. However, when the user creates a new entry with one class, and then the other, the previous classes input is created again, thus causing a duplicate in data.
I’m not exactly sure what’s going on and looking for some help:
Here’s my general dialog box class:
class NewDataDialog(wx.Dialog):
def __init__(self, parent, id, title, databoxes, pubsubmessage):
wx.Dialog.__init__(self,parent,id, title)
self.databoxes = databoxes
self.pubsubmessage = pubsubmessage
# MainSizer Creation
self.MainSizer = wx.BoxSizer(wx.VERTICAL)
# Top Label Creation and addition to Main Sizer
self.TopLbl = wx.StaticText(self, -1, "Create a " + title)
self.TopLbl.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
self.MainSizer.Add(self.TopLbl, 0, wx.ALL|wx.ALIGN_CENTER, 5)
self.SubSizer = wx.BoxSizer(wx.HORIZONTAL)
# Labels
self.LblSizer = wx.BoxSizer(wx.VERTICAL)
for lbl in databoxes:
self.lbl = wx.StaticText(self, -1, lbl+":")
self.LblSizer.Add(self.lbl, 0, wx.ALL|wx.ALIGN_LEFT, 5)
self.SubSizer.Add(self.LblSizer, 0, wx.ALL|wx.ALIGN_LEFT, 5)
# Boxes
self.InputSizer = wx.BoxSizer(wx.VERTICAL)
for lbl in databoxes:
self.box = wx.TextCtrl(self, -1, style=wx.ALIGN_LEFT)
self.InputSizer.Add(self.box, 0, wx.ALL, 1)
self.SubSizer.Add(self.InputSizer, 0, wx.ALL|wx.ALIGN_LEFT, 5)
self.MainSizer.Add(self.SubSizer, 0, wx.ALL|wx.ALIGN_CENTER, 5)
# Save and Cancel Buttons
self.buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
self.saveButton = SaveButton(self, 'Save')
self.cancelButton = CancelButton(self, 'Cancel')
self.buttonSizer.Add(self.saveButton, 0, wx.ALL|wx.ALIGN_CENTER, 5)
self.buttonSizer.Add(self.cancelButton, 0, wx.ALL|wx.ALIGN_CENTER, 5)
self.MainSizer.Add(self.buttonSizer, 0, wx.ALL|wx.ALIGN_CENTER, 5)
self.SetSizerAndFit(self.MainSizer)
pub.subscribe(self.cancelbutton, "mainpanel.CancelButton")
pub.subscribe(self.savebutton, "mainpanel.SaveButton")
def cancelbutton(self, message):
self.Close()
def savebutton(self, message):
results = []
for lbl in self.databoxes:
results.append(self.box.GetValue())
pub.sendMessage(self.pubsubmessage, results)
self.Close()
class CancelButton(wx.Panel):
def __init__(self, parent, title):
wx.Panel.__init__(self, parent)
self.dialogbutton = wx.Button(self, -1, title)
self.Bind(wx.EVT_BUTTON, self.OnButtonActivate, self.dialogbutton)
def OnButtonActivate(self, event):
pub.sendMessage("mainpanel.CancelButton", "")
class SaveButton(CancelButton):
def OnButtonActivate(self, event):
pub.sendMessage("mainpanel.SaveButton", "")
And here’s the creation of each instance of the dialog box:
def newsubject(self, message):
titles = ["Full Name", "Initials", "Job Title"]
self.newsubject = NewDataDialog(self, 0, 'New Subject', titles, "MainPanel.NewSubjectDialog.Save")
self.newsubject.ShowModal()
def newobserver(self, message):
titles = ["Full Name", "Initials"]
self.newobserver = NewDataDialog(self, 1, 'New Observer', titles, "MainPanel.NewObserverDialog.Save")
self.newobserver.ShowModal()
def newclinic(self, message):
titles = ["Clinic Name","Location","Initials"]
self.newclinic = NewDataDialog(self, 2, 'New Clinic', titles, "MainPanel.NewClinicDialog.Save")
self.newclinic.ShowModal()
Where each of these are called if the user selects a button labeled for the action that they desire. I.e. “Create a New Observer” will run the function newobserver
For a full view of the code you can look here. Note: Not all comments are correctly annotated, and there are other files as a part of this and so therefore won’t run, but will hopefully give you a little more insight to what’s going on
You are storing references to the
NewDataDialoginstances inself.newsubject,self.newobserverandself.newclinic. This means the objects continue to exist, and to listen for"mainpanel.SaveButton"events, even after thenewsubject,newobserver, andnewclinicmethods exit. Once you’ve opened one of each kind of dialog box, there will always be three such objects hanging around, and each"mainpanel.SaveButton"event will cause all three to try and save their data.If you did not store these references, then the objects would apparently be garbage collected, and the subscriptions automatically removed. If you do need to store the references, then you should arrange for an
unsubscribeto happen after theShowModal().