[9520702] | 1 | """ |
---|
[0efca36] | 2 | This module provide GUI for the mass density calculator |
---|
[9520702] | 3 | |
---|
| 4 | """ |
---|
| 5 | import wx |
---|
| 6 | import sys |
---|
[79492222] | 7 | from sas.guiframe.panel_base import PanelBase |
---|
[9520702] | 8 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
[79492222] | 9 | from sas.guiframe.utils import check_float |
---|
[49ab5d7] | 10 | from sas.guiframe.events import StatusEvent |
---|
[9520702] | 11 | from periodictable import formula as Formula |
---|
[79492222] | 12 | from sas.perspectives.calculator import calculator_widgets as widget |
---|
[58c125d] | 13 | from sas.guiframe.documentation_window import DocumentationWindow |
---|
[49ab5d7] | 14 | |
---|
| 15 | AVOGADRO = 6.02214129e23 |
---|
[9520702] | 16 | _INPUTS = ['Mass Density', 'Molar Volume'] |
---|
| 17 | _UNITS = ['g/cm^(3) ', 'cm^(3)/mol '] |
---|
[0efca36] | 18 | #Density panel size |
---|
[9520702] | 19 | if sys.platform.count("win32") > 0: |
---|
[d5419f7f] | 20 | PANEL_TOP = 0 |
---|
[9520702] | 21 | _STATICBOX_WIDTH = 410 |
---|
| 22 | _BOX_WIDTH = 200 |
---|
| 23 | PANEL_SIZE = 440 |
---|
| 24 | FONT_VARIANT = 0 |
---|
| 25 | else: |
---|
[d5419f7f] | 26 | PANEL_TOP = 60 |
---|
[9520702] | 27 | _STATICBOX_WIDTH = 430 |
---|
| 28 | _BOX_WIDTH = 200 |
---|
| 29 | PANEL_SIZE = 460 |
---|
| 30 | FONT_VARIANT = 1 |
---|
[49ab5d7] | 31 | |
---|
[9520702] | 32 | class DensityPanel(ScrolledPanel, PanelBase): |
---|
| 33 | """ |
---|
| 34 | Provides the mass density calculator GUI. |
---|
| 35 | """ |
---|
| 36 | ## Internal nickname for the window, used by the AUI manager |
---|
| 37 | window_name = "Mass Density Calculator" |
---|
| 38 | ## Name to appear on the window title bar |
---|
| 39 | window_caption = "Mass Density Calculator" |
---|
| 40 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
| 41 | CENTER_PANE = True |
---|
[49ab5d7] | 42 | |
---|
[9520702] | 43 | def __init__(self, parent, base=None, *args, **kwds): |
---|
| 44 | """ |
---|
| 45 | """ |
---|
| 46 | ScrolledPanel.__init__(self, parent, *args, **kwds) |
---|
| 47 | PanelBase.__init__(self) |
---|
| 48 | self.SetupScrolling() |
---|
| 49 | #Font size |
---|
| 50 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 51 | # Object that receive status event |
---|
| 52 | self.base = base |
---|
[d5419f7f] | 53 | self.parent = parent |
---|
[9520702] | 54 | # chemeical formula, string |
---|
| 55 | self.compound = '' |
---|
| 56 | # value of the density/volume, float |
---|
| 57 | self.input = None |
---|
| 58 | # text controls |
---|
| 59 | self.compound_ctl = None |
---|
| 60 | self.input_ctl = None |
---|
| 61 | self.molar_mass_ctl = None |
---|
| 62 | self.output_ctl = None |
---|
| 63 | self.ctr_color = self.GetBackgroundColour() |
---|
| 64 | # button |
---|
| 65 | self.button_calculate = None |
---|
| 66 | # list |
---|
| 67 | self._input_list = _INPUTS |
---|
| 68 | self._input = self._input_list[1] |
---|
| 69 | self._output = self._input_list[0] |
---|
| 70 | self._unit_list = _UNITS |
---|
| 71 | #Draw the panel |
---|
| 72 | self._do_layout() |
---|
| 73 | self.SetAutoLayout(True) |
---|
| 74 | self.Layout() |
---|
[49ab5d7] | 75 | |
---|
[9520702] | 76 | def _do_layout(self): |
---|
| 77 | """ |
---|
| 78 | Draw window content |
---|
| 79 | """ |
---|
| 80 | # units |
---|
| 81 | unit_density = self._unit_list[0] |
---|
| 82 | unit_volume = self._unit_list[1] |
---|
[49ab5d7] | 83 | |
---|
[9520702] | 84 | # sizers |
---|
| 85 | sizer_input = wx.GridBagSizer(5, 5) |
---|
| 86 | sizer_output = wx.GridBagSizer(5, 5) |
---|
| 87 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 88 | self.sizer1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 89 | self.sizer2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 90 | sizer3 = wx.BoxSizer(wx.HORIZONTAL) |
---|
[49ab5d7] | 91 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 92 | |
---|
[9520702] | 93 | # inputs |
---|
[992b594] | 94 | inputbox = wx.StaticBox(self, -1, "Inputs") |
---|
[9520702] | 95 | boxsizer1 = wx.StaticBoxSizer(inputbox, wx.VERTICAL) |
---|
| 96 | boxsizer1.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
| 97 | compound_txt = wx.StaticText(self, -1, 'Molecular Formula ') |
---|
| 98 | self.compound_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
| 99 | self.compound_eg1 = wx.StaticText(self, -1, ' e.g., H2O') |
---|
| 100 | self.compound_eg2 = wx.StaticText(self, -1, 'e.g., D2O') |
---|
| 101 | self.input_cb = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
[49ab5d7] | 102 | wx.EVT_COMBOBOX(self.input_cb, -1, self.on_select_input) |
---|
[9520702] | 103 | hint_input_name_txt = 'Mass or volume.' |
---|
[49ab5d7] | 104 | self.input_cb.SetToolTipString(hint_input_name_txt) |
---|
[9520702] | 105 | unit_density1 = " " + unit_density |
---|
| 106 | self.input_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
| 107 | self.unit_input_density = wx.StaticText(self, -1, unit_density1) |
---|
| 108 | self.unit_input_volume = wx.StaticText(self, -1, unit_volume) |
---|
| 109 | iy = 0 |
---|
| 110 | ix = 0 |
---|
| 111 | sizer_input.Add(compound_txt, (iy, ix), (1, 1), |
---|
[49ab5d7] | 112 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[9520702] | 113 | ix += 1 |
---|
| 114 | sizer_input.Add(self.compound_ctl, (iy, ix), (1, 1), |
---|
[49ab5d7] | 115 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[9520702] | 116 | ix += 1 |
---|
| 117 | sizer_input.Add(self.compound_eg1, (iy, ix), (1, 1), |
---|
[49ab5d7] | 118 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 119 | |
---|
[9520702] | 120 | ix += 1 |
---|
| 121 | sizer_input.Add(self.compound_eg2, (iy, ix), (1, 1), |
---|
[49ab5d7] | 122 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[9520702] | 123 | self.compound_eg1.Show(False) |
---|
| 124 | iy += 1 |
---|
| 125 | ix = 0 |
---|
| 126 | sizer_input.Add(self.input_cb, (iy, ix), (1, 1), |
---|
[49ab5d7] | 127 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[9520702] | 128 | ix += 1 |
---|
| 129 | sizer_input.Add(self.input_ctl, (iy, ix), (1, 1), |
---|
[49ab5d7] | 130 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 131 | ix += 1 |
---|
| 132 | sizer_input.Add(self.unit_input_density, (iy, ix), (1, 1), |
---|
| 133 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 134 | ix += 1 |
---|
[9520702] | 135 | self.unit_input_density.Show(False) |
---|
[49ab5d7] | 136 | sizer_input.Add(self.unit_input_volume, (iy, ix), (1, 1), |
---|
| 137 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[9520702] | 138 | boxsizer1.Add(sizer_input) |
---|
| 139 | self.sizer1.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
[49ab5d7] | 140 | |
---|
[9520702] | 141 | # outputs |
---|
[992b594] | 142 | outputbox = wx.StaticBox(self, -1, "Outputs") |
---|
[9520702] | 143 | boxsizer2 = wx.StaticBoxSizer(outputbox, wx.VERTICAL) |
---|
| 144 | boxsizer2.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
[49ab5d7] | 145 | |
---|
[9520702] | 146 | molar_mass_txt = wx.StaticText(self, -1, 'Molar Mass ') |
---|
| 147 | self.molar_mass_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
| 148 | self.molar_mass_ctl.SetEditable(False) |
---|
| 149 | self.molar_mass_ctl.SetBackgroundColour(self.ctr_color) |
---|
| 150 | self.molar_mass_unit1 = wx.StaticText(self, -1, ' g/mol') |
---|
| 151 | self.molar_mass_unit2 = wx.StaticText(self, -1, 'g/mol') |
---|
[49ab5d7] | 152 | |
---|
[9520702] | 153 | self.output_cb = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
[49ab5d7] | 154 | wx.EVT_COMBOBOX(self.output_cb, -1, self.on_select_output) |
---|
[9520702] | 155 | hint_output_name_txt = 'Mass or volume.' |
---|
[49ab5d7] | 156 | self.output_cb.SetToolTipString(hint_output_name_txt) |
---|
[9520702] | 157 | list = [] |
---|
| 158 | for item in self._input_list: |
---|
| 159 | name = str(item) |
---|
| 160 | list.append(name) |
---|
| 161 | list.sort() |
---|
| 162 | for idx in range(len(list)): |
---|
[49ab5d7] | 163 | self.input_cb.Append(list[idx], idx) |
---|
| 164 | self.output_cb.Append(list[idx], idx) |
---|
| 165 | self.input_cb.SetStringSelection("Molar Volume") |
---|
| 166 | self.output_cb.SetStringSelection("Mass Density") |
---|
[9520702] | 167 | unit_volume = " " + unit_volume |
---|
| 168 | self.output_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
| 169 | self.output_ctl.SetEditable(False) |
---|
| 170 | self.output_ctl.SetBackgroundColour(self.ctr_color) |
---|
| 171 | self.unit_output_density = wx.StaticText(self, -1, unit_density) |
---|
| 172 | self.unit_output_volume = wx.StaticText(self, -1, unit_volume) |
---|
| 173 | iy = 0 |
---|
| 174 | ix = 0 |
---|
| 175 | sizer_output.Add(molar_mass_txt, (iy, ix), (1, 1), |
---|
[49ab5d7] | 176 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[9520702] | 177 | ix += 1 |
---|
| 178 | sizer_output.Add(self.molar_mass_ctl, (iy, ix), (1, 1), |
---|
[49ab5d7] | 179 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[9520702] | 180 | ix += 1 |
---|
| 181 | sizer_output.Add(self.molar_mass_unit1, (iy, ix), (1, 1), |
---|
[49ab5d7] | 182 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[9520702] | 183 | ix += 1 |
---|
| 184 | sizer_output.Add(self.molar_mass_unit2, (iy, ix), (1, 1), |
---|
[49ab5d7] | 185 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[9520702] | 186 | self.molar_mass_unit1.Show(False) |
---|
| 187 | iy += 1 |
---|
| 188 | ix = 0 |
---|
| 189 | sizer_output.Add(self.output_cb, (iy, ix), (1, 1), |
---|
[49ab5d7] | 190 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[9520702] | 191 | ix += 1 |
---|
| 192 | sizer_output.Add(self.output_ctl, (iy, ix), (1, 1), |
---|
[49ab5d7] | 193 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 194 | ix += 1 |
---|
[9520702] | 195 | sizer_output.Add(self.unit_output_volume, |
---|
[49ab5d7] | 196 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[9520702] | 197 | ix += 1 |
---|
| 198 | sizer_output.Add(self.unit_output_density, |
---|
[49ab5d7] | 199 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 200 | |
---|
[9520702] | 201 | self.unit_output_volume.Show(False) |
---|
| 202 | boxsizer2.Add(sizer_output) |
---|
[49ab5d7] | 203 | self.sizer2.Add(boxsizer2, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 204 | |
---|
[9520702] | 205 | # buttons |
---|
| 206 | id = wx.NewId() |
---|
| 207 | self.button_calculate = wx.Button(self, id, "Calculate") |
---|
| 208 | self.button_calculate.SetToolTipString("Calculate.") |
---|
[49ab5d7] | 209 | self.Bind(wx.EVT_BUTTON, self.calculate, id=id) |
---|
| 210 | |
---|
[58c125d] | 211 | id = wx.NewId() |
---|
| 212 | self.button_help = wx.Button(self, id, "HELP") |
---|
| 213 | self.button_help.SetToolTipString("Help for density calculator.") |
---|
[49ab5d7] | 214 | self.Bind(wx.EVT_BUTTON, self.on_help, id=id) |
---|
| 215 | |
---|
[d5419f7f] | 216 | self.button_close = wx.Button(self, wx.ID_CANCEL, 'Close') |
---|
| 217 | self.button_close.Bind(wx.EVT_BUTTON, self.on_close) |
---|
| 218 | self.button_close.SetToolTipString("Close this window.") |
---|
| 219 | |
---|
| 220 | sizer_button.Add((100, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[49ab5d7] | 221 | sizer_button.Add(self.button_calculate, 0, |
---|
| 222 | wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
| 223 | sizer_button.Add(self.button_help, 0, |
---|
| 224 | wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
[d5419f7f] | 225 | sizer_button.Add(self.button_close, 0, |
---|
| 226 | wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
[9520702] | 227 | sizer3.Add(sizer_button) |
---|
[49ab5d7] | 228 | |
---|
[9520702] | 229 | # layout |
---|
| 230 | vbox.Add(self.sizer1) |
---|
| 231 | vbox.Add(self.sizer2) |
---|
| 232 | vbox.Add(sizer3) |
---|
[49ab5d7] | 233 | vbox.Fit(self) |
---|
[9520702] | 234 | self.SetSizer(vbox) |
---|
[49ab5d7] | 235 | |
---|
[9520702] | 236 | def on_select_input(self, event): |
---|
| 237 | """ |
---|
[49ab5d7] | 238 | On selection of input combobox, |
---|
[9520702] | 239 | update units and output combobox |
---|
| 240 | """ |
---|
| 241 | if event == None: |
---|
| 242 | return |
---|
| 243 | event.Skip() |
---|
[49ab5d7] | 244 | |
---|
[9520702] | 245 | combo = event.GetEventObject() |
---|
| 246 | self._input = combo.GetValue() |
---|
| 247 | for name in self._input_list: |
---|
| 248 | if self._input != name: |
---|
| 249 | self._output = name |
---|
| 250 | break |
---|
| 251 | |
---|
| 252 | self.set_values() |
---|
[49ab5d7] | 253 | |
---|
[9520702] | 254 | def on_select_output(self, event): |
---|
| 255 | """ |
---|
[49ab5d7] | 256 | On selection of output combobox, |
---|
[9520702] | 257 | update units and input combobox |
---|
| 258 | """ |
---|
| 259 | if event == None: |
---|
| 260 | return |
---|
| 261 | event.Skip() |
---|
[49ab5d7] | 262 | |
---|
[9520702] | 263 | combo = event.GetEventObject() |
---|
| 264 | self._output = combo.GetValue() |
---|
| 265 | for name in self._input_list: |
---|
| 266 | if self._output != name: |
---|
| 267 | self._input = name |
---|
| 268 | break |
---|
| 269 | |
---|
| 270 | self.set_values() |
---|
[49ab5d7] | 271 | |
---|
[9520702] | 272 | def set_values(self): |
---|
| 273 | """ |
---|
| 274 | Sets units and combobox values |
---|
| 275 | """ |
---|
| 276 | input, output = self.get_input() |
---|
| 277 | if input is None: |
---|
| 278 | return |
---|
| 279 | # input |
---|
| 280 | self.input_cb.SetValue(str(input)) |
---|
| 281 | # output |
---|
| 282 | self.output_cb.SetValue(str(output)) |
---|
| 283 | # unit |
---|
| 284 | if self._input_list.index(input) == 0: |
---|
| 285 | self.molar_mass_unit1.Show(True) |
---|
| 286 | self.molar_mass_unit2.Show(False) |
---|
| 287 | self.compound_eg1.Show(True) |
---|
| 288 | self.compound_eg2.Show(False) |
---|
| 289 | self.unit_input_density.Show(True) |
---|
| 290 | self.unit_output_volume.Show(True) |
---|
| 291 | self.unit_input_volume.Show(False) |
---|
[49ab5d7] | 292 | self.unit_output_density.Show(False) |
---|
[9520702] | 293 | else: |
---|
| 294 | self.molar_mass_unit1.Show(False) |
---|
| 295 | self.molar_mass_unit2.Show(True) |
---|
| 296 | self.compound_eg1.Show(False) |
---|
| 297 | self.compound_eg2.Show(True) |
---|
| 298 | self.unit_input_volume.Show(True) |
---|
| 299 | self.unit_output_density.Show(True) |
---|
| 300 | self.unit_input_density.Show(False) |
---|
| 301 | self.unit_output_volume.Show(False) |
---|
| 302 | # layout |
---|
| 303 | self.clear_outputs() |
---|
[49ab5d7] | 304 | self.sizer1.Layout() |
---|
| 305 | self.sizer2.Layout() |
---|
| 306 | |
---|
[9520702] | 307 | def get_input(self): |
---|
| 308 | """ |
---|
| 309 | Return the current input and output combobox values |
---|
| 310 | """ |
---|
| 311 | return self._input, self._output |
---|
[49ab5d7] | 312 | |
---|
[9520702] | 313 | def check_inputs(self): |
---|
| 314 | """ |
---|
| 315 | Check validity user inputs |
---|
| 316 | """ |
---|
| 317 | flag = True |
---|
| 318 | msg = "" |
---|
| 319 | if check_float(self.input_ctl): |
---|
| 320 | self.input = float(self.input_ctl.GetValue()) |
---|
| 321 | else: |
---|
| 322 | flag = False |
---|
| 323 | input_type = str(self.input_cb.GetValue()) |
---|
[49ab5d7] | 324 | msg += "Error for %s value :expect float" % input_type |
---|
| 325 | |
---|
[9520702] | 326 | self.compound = self.compound_ctl.GetValue().lstrip().rstrip() |
---|
| 327 | if self.compound != "": |
---|
| 328 | try : |
---|
| 329 | Formula(self.compound) |
---|
| 330 | self.compound_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 331 | self.compound_ctl.Refresh() |
---|
| 332 | except: |
---|
| 333 | self.compound_ctl.SetBackgroundColour("pink") |
---|
| 334 | self.compound_ctl.Refresh() |
---|
| 335 | flag = False |
---|
| 336 | msg += "Enter correct formula" |
---|
| 337 | else: |
---|
| 338 | self.compound_ctl.SetBackgroundColour("pink") |
---|
| 339 | self.compound_ctl.Refresh() |
---|
| 340 | flag = False |
---|
| 341 | msg += "Enter Formula" |
---|
| 342 | return flag, msg |
---|
[49ab5d7] | 343 | |
---|
[9520702] | 344 | |
---|
| 345 | def calculate(self, event): |
---|
| 346 | """ |
---|
| 347 | Calculate the mass Density/molar Volume of the molecules |
---|
| 348 | """ |
---|
| 349 | self.clear_outputs() |
---|
| 350 | try: |
---|
| 351 | #Check validity user inputs |
---|
| 352 | flag, msg = self.check_inputs() |
---|
| 353 | if self.base is not None and msg.lstrip().rstrip() != "": |
---|
| 354 | msg = "Density/Volume Calculator: %s" % str(msg) |
---|
| 355 | wx.PostEvent(self.base, StatusEvent(status=msg)) |
---|
| 356 | if not flag: |
---|
[49ab5d7] | 357 | return |
---|
[9520702] | 358 | #get ready to compute |
---|
| 359 | mol_formula = Formula(self.compound) |
---|
| 360 | molar_mass = float(mol_formula.molecular_mass) * AVOGADRO |
---|
| 361 | output = self._format_number(molar_mass / self.input) |
---|
| 362 | self.molar_mass_ctl.SetValue(str(self._format_number(molar_mass))) |
---|
| 363 | self.output_ctl.SetValue(str(output)) |
---|
| 364 | except: |
---|
| 365 | if self.base is not None: |
---|
[49ab5d7] | 366 | msg = "Density/Volume Calculator: %s" % (sys.exc_value) |
---|
[9520702] | 367 | wx.PostEvent(self.base, StatusEvent(status=msg)) |
---|
| 368 | if event is not None: |
---|
| 369 | event.Skip() |
---|
[49ab5d7] | 370 | |
---|
| 371 | def on_help(self, event): |
---|
[58c125d] | 372 | """ |
---|
| 373 | Bring up the density/volume calculator Documentation whenever |
---|
[49ab5d7] | 374 | the HELP button is clicked. |
---|
| 375 | |
---|
[58c125d] | 376 | Calls DocumentationWindow with the path of the location within the |
---|
[49ab5d7] | 377 | documentation tree (after /doc/ ....". Note that when using old |
---|
| 378 | versions of Wx (before 2.9) and thus not the release version of |
---|
| 379 | installers, the help comes up at the top level of the file as |
---|
[58c125d] | 380 | webbrowser does not pass anything past the # to the browser when it is |
---|
| 381 | running "file:///...." |
---|
[49ab5d7] | 382 | |
---|
[58c125d] | 383 | :param evt: Triggers on clicking the help button |
---|
| 384 | """ |
---|
[49ab5d7] | 385 | |
---|
[58c125d] | 386 | _TreeLocation = "user/perspectives/calculator/density_calculator_help.html" |
---|
[3db44fb] | 387 | _doc_viewer = DocumentationWindow(self, -1, _TreeLocation, "", |
---|
| 388 | "Density/Volume Calculator Help") |
---|
[58c125d] | 389 | |
---|
[d5419f7f] | 390 | def on_close(self, event): |
---|
| 391 | """ |
---|
| 392 | close the window containing this panel |
---|
| 393 | """ |
---|
| 394 | self.parent.Close() |
---|
| 395 | |
---|
[9520702] | 396 | def clear_outputs(self): |
---|
| 397 | """ |
---|
| 398 | Clear the outputs textctrl |
---|
| 399 | """ |
---|
| 400 | self.molar_mass_ctl.SetValue("") |
---|
| 401 | self.output_ctl.SetValue("") |
---|
[49ab5d7] | 402 | |
---|
[9520702] | 403 | def _format_number(self, value=None): |
---|
| 404 | """ |
---|
[49ab5d7] | 405 | Return a float in a standardized, human-readable formatted string |
---|
[9520702] | 406 | """ |
---|
[49ab5d7] | 407 | try: |
---|
[9520702] | 408 | value = float(value) |
---|
| 409 | except: |
---|
| 410 | output = '' |
---|
| 411 | return output |
---|
| 412 | |
---|
| 413 | output = "%-12.5f" % value |
---|
[49ab5d7] | 414 | return output.lstrip().rstrip() |
---|
| 415 | |
---|
[d0923a3] | 416 | class DensityWindow(widget.CHILD_FRAME): |
---|
[9520702] | 417 | """ |
---|
| 418 | """ |
---|
| 419 | def __init__(self, parent=None, title="Density/Volume Calculator", |
---|
[49ab5d7] | 420 | base=None, manager=None, |
---|
| 421 | size=(PANEL_SIZE * 1.05, PANEL_SIZE / 1.55), *args, **kwds): |
---|
[9520702] | 422 | """ |
---|
| 423 | """ |
---|
| 424 | kwds['title'] = title |
---|
| 425 | kwds['size'] = size |
---|
[d0923a3] | 426 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwds) |
---|
[9520702] | 427 | """ |
---|
| 428 | """ |
---|
[ae84427] | 429 | self.manager = manager |
---|
[9520702] | 430 | self.panel = DensityPanel(self, base=base) |
---|
[ae84427] | 431 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
[d5419f7f] | 432 | self.SetPosition((wx.LEFT, PANEL_TOP)) |
---|
[9520702] | 433 | self.Show(True) |
---|
[49ab5d7] | 434 | |
---|
[ae84427] | 435 | def on_close(self, event): |
---|
| 436 | """ |
---|
| 437 | On close event |
---|
| 438 | """ |
---|
| 439 | if self.manager != None: |
---|
| 440 | self.manager.cal_md_frame = None |
---|
| 441 | self.Destroy() |
---|
[49ab5d7] | 442 | |
---|
| 443 | |
---|
[9520702] | 444 | class ViewApp(wx.App): |
---|
| 445 | """ |
---|
| 446 | """ |
---|
| 447 | def OnInit(self): |
---|
| 448 | """ |
---|
| 449 | """ |
---|
[62af27a9] | 450 | widget.CHILD_FRAME = wx.Frame |
---|
[49ab5d7] | 451 | frame = DensityWindow(None, title="Density/Volume Calculator") |
---|
[9520702] | 452 | frame.Show(True) |
---|
| 453 | self.SetTopWindow(frame) |
---|
| 454 | return True |
---|
| 455 | |
---|
[49ab5d7] | 456 | |
---|
| 457 | if __name__ == "__main__": |
---|
[9520702] | 458 | app = ViewApp(0) |
---|
[49ab5d7] | 459 | app.MainLoop() |
---|