Ejemplo n.º 1
0
class WaFF(BoxLayout):

    def __init__(self, *args, **kwargs):
        super(BoxLayout, self).__init__(*args, **kwargs)
        self.textinput = TextInput(text='Enter elements separated by spaces', multiline=False,
                                   size=(300, 50))
        self.rut = Label(text='', pos=(450, 250))
        self.add_widget(self.textinput)
        self.textinput.bind(on_text_validate=self.chicken)
        self.wextinput = TextInput(text='Enter given', multiline=False, size=(300, 50))
        self.wextinput.bind(on_text_validate=self.alfredo)
        self.wextinput.bind(focus=self.on_focus2)
        self.reset = TextInput(text='Type reset to reset', multiline=False, size=(300, 50))
        self.texts = [self.textinput, self.wextinput, self.reset]
        self.textinput.bind(focus=self.on_focus)
        self.reset.bind(on_text_validate=self.resets)
        self.given = 0
        self.atoms = 0
        self.but = Label(text='Given: ' + str(self.given), pos=(425, 200))
        self.tut = Label(text='' + str(self.atoms), pos=(425, 225))
        self.mmlabel = Label(pos=((self.textinput.x + 50), (self.textinput.y + 100)))
        self.atlabel = Label(pos=(350, 425))
        self.molabel = Label(pos=(350, 400))
        self.tolabel = Label(pos=(350, 450))
        self.errorlabel = Label(pos=((self.textinput.x + 100), (self.textinput.y + 100)))
        self.add_widget(self.but)
        self.add_widget(self.tut)
        self.add_widget(self.atlabel)
        self.add_widget(self.molabel)
        self.add_widget(self.tolabel)
        self.mm = []

    def chicken(self, *args):
        msg = self.textinput.text
        ci = msg.split()
        tos = 0
        try:
            self.remove_widget(self.textinput)
        except:
            pass
        try:
            self.add_widget(self.wextinput)
        except:
            pass
        a = Button(text='Calculate', pos=(600, 150), background_color=(.5, .2, .5, 1))
        a.bind(on_release=partial(self.currentValue, self.calc(ci)))
        self.add_widget(a)

    def alfredo(self, *args):
        try:
            float(self.wextinput.text)
            self.given = self.wextinput.text
            self.but.text = 'Given: ' + str(self.given)
            self.wextinput.text = 'Enter atoms'
            self.wextinput.unbind(on_text_validate=self.alfredo)
        except ValueError:
            self.wextinput.text = 'Enter given'

    def calc(self, alist):
        try:
            wnum = []
            for i in alist:
                if has_num(i):
                    wnum.append(i)
                else:
                    wnum.append(i + '1')
            intel = 0
            for i in wnum:
                if len(i) == 2:
                    intel += (float(rar[i[0]].mass) * float(i[1]))
                elif len(i) == 3:
                    if i[1].isdigit():
                        intel += float(rar[i[0]].mass) * float(i[1] + i[2])
                    else:
                        intel += float(rar[i[0] + i[1]].mass) * float(i[2])
            self.mmlabel.text = 'molar mass: ' + str(intel)
            self.add_widget(self.mmlabel)
            try:
                self.remove_widget(self.errorlabel)
            except:
                pass
            return intel
        except KeyError:
            self.errorlabel.text = 'Invalid input, enter elements again'
            try:
                self.add_widget(self.errorlabel)
            except:
                pass
            try:
                self.remove_widget(self.wextinput)
                self.add_widget(self.textinput)
            except:
                pass

    def currentValue(self, x, *args):
        ele = Element(x, self.given, self.atoms)
        atoms = float(ele.atoms) * float(ele.molecules)
        self.atlabel.text = 'atoms: ' + str(atoms)
        self.molabel.text = 'molecules: ' + str(ele.molecules)
        self.tolabel.text = 'moles: ' + str(ele.moles)
        self.remove_widget(self.wextinput)
        try:
            self.add_widget(self.reset)
        except:
            pass

    def resets(self, *args):
        self.clear_widgets()
        try:
            self.add_widget(self.textinput)
        except:
            pass
        self.textinput.bind(on_text_validate=self.chicken)
        self.given = 0
        self.atoms = 0
        self.reset.unbind(on_text_validate=self.resets)
        self.add_widget(self.but)
        self.add_widget(self.tut)
        self.but.text = 'Given: ' + str(self.given)
        self.tut.text = 'Atoms: ' + str(self.atoms)

    def on_focus(self, instance, value, *args):
        if value:
            self.textinput.text = ''

    def on_focus2(self, instance, value, *args):
        if value:
            self.wextinput.text = ''
Ejemplo n.º 2
0
class AcidBase(BoxLayout):

    def __init__(self, *args, **kwargs):
        super(BoxLayout, self).__init__(*args, **kwargs)
        self.chemt = TextInput(text='1. pH, 2. H+, 3. OH-, 4. pOH', multiline=False,
                               size=(300, 50), input_filter=float)
        self.chemt.bind(focus=self.on_focus)
        self.chemt.bind(on_text_validate=self.setab)
        self.acidbase = 0
        self.errorlab = Label(pos=(self.chemt.x + 50, (self.chemt.y + 100)))
        self.ph = Label(pos=(350, 375))
        self.h = Label(pos=(350, 400))
        self.oh = Label(pos=(350, 425))
        self.poh = Label(pos=(350, 450))
        self.labels = [self.ph, self.h, self.oh, self.poh]
        for i in self.labels:
            self.add_widget(i)
        self.add_widget(self.chemt)

    def setab(self, *args):
        self.acidbase = self.chemt.text
        if self.acidbase == '1':
            self.chemt.text = 'Enter pH value'
            self.chemt.unbind(on_text_validate=self.setab)
            self.chemt.bind(on_text_validate=partial(self.calc, 1))
        elif self.acidbase == '2':
            self.chemt.text = 'Enter H+ value'
            self.chemt.unbind(on_text_validate=self.setab)
            self.chemt.bind(on_text_validate=partial(self.calc, 2))
        elif self.acidbase == '3':
            self.chemt.text = 'Enter OH- value'
            self.chemt.unbind(on_text_validate=self.setab)
            self.chemt.bind(on_text_validate=partial(self.calc, 3))
        elif self.acidbase == '4':
            self.chemt.text = 'Enter pOH value'
            self.chemt.unbind(on_text_validate=self.setab)
            self.chemt.bind(on_text_validate=partial(self.calc, 4))

    def calc(self, x, *args):
        if x == 1:
            try:
                theph = float(self.chemt.text)
                self.ph.text = 'pH = ' + str(theph)
                self.h.text = 'H+ = ' + phtoh(theph)
                self.oh.text = 'OH- = ' + htoh(phtoh(theph))
                self.poh.text = 'pOH = ' + str((14 - theph))
                try:
                    self.remove_widget(self.errorlab)
                except:
                    pass
            except ValueError:
                self.errorlab.text = 'Invalid, enter pH again'
                try:
                    self.add_widget(self.errorlab)
                except:
                    pass
        if x == 2:
                self.ph.text = 'pH = ' + ohtoph(float(self.chemt.text))
                self.h.text = 'H+ = ' + str('%.2E' % float(self.chemt.text))
                self.oh.text = 'OH- = ' + htoh(float(self.chemt.text))
                self.poh.text = 'pOH = ' + str(14 - float(ohtoph(float(self.chemt.text))))
                try:
                    self.remove_widget(self.errorlab)
                except:
                    pass
        if x == 3:
            try:
                he = float(constant) / float(self.chemt.text)
                ph2 = -1 * math.log10(he)
                self.ph.text = 'pH = ' + str(float(ph2))
                self.h.text = 'H+ = ' + str('%.2E' % float(he))
                self.oh.text = 'OH- = ' + str('%.2E' % float(self.chemt.text))
                self.poh.text = 'pOH = ' + str(14 - float(ph2))
                try:
                    self.remove_widget(self.errorlab)
                except:
                    pass
            except ValueError:
                self.errorlab.text = 'Invalid, enter OH- again'
                try:
                    self.add_widget(self.errorlab)
                except:
                    pass
        if x == 4:
            try:
                ohh = 10**(-float(self.chemt.text))
                he = float(constant) / float(ohh)
                self.ph.text = 'pH = ' + str(14 - float(self.chemt.text))
                self.h.text = 'H+ = ' + str('%.2E' % float(he))
                self.oh.text = 'OH- = ' + str('%.2E' % float(ohh))
                self.poh.text = 'pOH = ' + self.chemt.text
                try:
                    self.remove_widget(self.errorlab)
                except:
                    pass
            except ValueError:
                self.errorlab.text = 'Invalid, enter pOH again'
                try:
                    self.add_widget(self.errorlab)
                except:
                    pass

    def on_focus(self, instance, value, *args):
        if value:
            self.chemt.text = ''