def calculate(self, event): """ Calculate the mass Density/molar Volume of the molecules """ self.clear_outputs() try: #Check validity user inputs flag, msg = self.check_inputs() if self.base is not None and msg.lstrip().rstrip() != "": msg = "Density/Volume Calculator: %s" % str(msg) wx.PostEvent(self.base, StatusEvent(status=msg)) if not flag: return #get ready to compute mol_formula = Formula(self.compound) molar_mass = float(mol_formula.molecular_mass) * AVOGADRO output = self._format_number(molar_mass / self.input) self.molar_mass_ctl.SetValue(str(self._format_number(molar_mass))) self.output_ctl.SetValue(str(output)) except Exception as exc: if self.base is not None: msg = "Density/Volume Calculator: %s" % exc wx.PostEvent(self.base, StatusEvent(status=msg)) if event is not None: event.Skip()
def check_inputs(self): """ Check validity user inputs """ flag = True msg = "" if check_float(self.input_ctl): self.input = float(self.input_ctl.GetValue()) else: flag = False input_type = str(self.input_cb.GetValue()) msg += "Error for %s value :expect float" % input_type self.compound = self.compound_ctl.GetValue().lstrip().rstrip() if self.compound != "": try: Formula(self.compound) self.compound_ctl.SetBackgroundColour(wx.WHITE) self.compound_ctl.Refresh() except: self.compound_ctl.SetBackgroundColour("pink") self.compound_ctl.Refresh() flag = False msg += "Enter correct formula" else: self.compound_ctl.SetBackgroundColour("pink") self.compound_ctl.Refresh() flag = False msg += "Enter Formula" return flag, msg