Temperature converter program

allan14 0 Tallied Votes 141 Views Share

Written using Boa Constructor, with code added to do the conversion from C to F or F to C.
The code has been altered to give a single file so don't open it in Boa Constructor.

#Boa:Frame:Frame1

import wx



def Convert_to_C(x):            #convert F to C
        x = (x-32)*5/9
        return x
    
def Convert_to_F(x):            #convert C to F
        x = (x*9/5)+32
        return x

[wxID_FRAME1, wxID_FRAME1CHOICE1, wxID_FRAME1PANEL1, wxID_FRAME1STATICTEXT1, 
 wxID_FRAME1STATICTEXT2, wxID_FRAME1STATICTEXT3, wxID_FRAME1STATICTEXT4, 
 wxID_FRAME1STATICTEXT5, wxID_FRAME1TEXTCTRL1, wxID_FRAME1TEXTCTRL2, 
] = [wx.NewId() for _init_ctrls in range(10)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(380, 327), size=wx.Size(510, 275),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Temperature converter')
        self.SetClientSize(wx.Size(510, 275))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(510, 275),
              style=wx.TAB_TRAVERSAL)

        self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
              label=u'Please enter a temperature to convert',
              name='staticText1', parent=self.panel1, pos=wx.Point(40, 70),
              size=wx.Size(246, 17), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self.panel1, pos=wx.Point(296, 64), size=wx.Size(80, 27),
              style=0, value=u'')

        self.staticText2 = wx.StaticText(id=wxID_FRAME1STATICTEXT2,
              label=u'Convert to degrees:', name='staticText2',
              parent=self.panel1, pos=wx.Point(161, 143), size=wx.Size(127, 17),
              style=0)

        self.choice1 = wx.Choice(choices=['Centigrade', 'Fahrenheit'],
              id=wxID_FRAME1CHOICE1, name='choice1', parent=self.panel1,
              pos=wx.Point(296, 136), size=wx.Size(152, 32), style=0)
        self.choice1.SetSelection(1)
        self.choice1.SetStringSelection(u'Centigrade, Fahrenheit')
        self.choice1.Bind(wx.EVT_CHOICE, self.OnChoice1Choice,
              id=wxID_FRAME1CHOICE1)

        self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2, name='textCtrl2',
              parent=self.panel1, pos=wx.Point(241, 218), size=wx.Size(99, 27),
              style=0, value=u'')
        self.textCtrl2.SetMinSize(wx.Size(49, 27))

        self.staticText3 = wx.StaticText(id=wxID_FRAME1STATICTEXT3, label=u'',
              name='staticText3', parent=self.panel1, pos=wx.Point(31, 224),
              size=wx.Size(45, 17), style=0)
        self.staticText3.SetMinSize(wx.Size(40, 17))

        self.staticText5 = wx.StaticText(id=wxID_FRAME1STATICTEXT5, label=u'',
              name='staticText5', parent=self.panel1, pos=wx.Point(90, 223),
              size=wx.Size(145, 17), style=0)

        self.staticText4 = wx.StaticText(id=wxID_FRAME1STATICTEXT4, label=u'',
              name='staticText4', parent=self.panel1, pos=wx.Point(351, 220),
              size=wx.Size(68, 17), style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
    
        

    def OnChoice1Choice(self, event):
        x = self.textCtrl1.GetValue()
        self.staticText3.SetLabel(x)
        x = float(x)             #convert to float
        ans = self.choice1.GetCurrentSelection()
        if ans == 0:             #first item in the list
            z = Convert_to_C(x)
            self.staticText5.SetLabel('degrees Fahrenheit = ')
            self.staticText4.SetLabel('degrees Centigrade')
        if ans == 1:             #second item in the list
            z = Convert_to_F(x)
            self.staticText5.SetLabel('degrees Centigrade = ')
            self.staticText4.SetLabel('degrees Fahrenheit')
        z = str(z)               #change to a string
        self.textCtrl2.SetValue(z)
        #event.Skip()
#.............................................................................

def create(parent):
    return Frame1(parent)

class BoaApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        self.main = create(None)
        self.main.Show()
        self.SetTopWindow(self.main)
        return True

def main():
    application = BoaApp(0)
    application.MainLoop()

if __name__ == '__main__':
    main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.