User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,758 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,452 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Nov 1st, 2007
Views: 2,005
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.
python Syntax
  1. #Boa:Frame:Frame1
  2.  
  3. import wx
  4.  
  5.  
  6.  
  7. def Convert_to_C(x): #convert F to C
  8. x = (x-32)*5/9
  9. return x
  10.  
  11. def Convert_to_F(x): #convert C to F
  12. x = (x*9/5)+32
  13. return x
  14.  
  15. [wxID_FRAME1, wxID_FRAME1CHOICE1, wxID_FRAME1PANEL1, wxID_FRAME1STATICTEXT1,
  16. wxID_FRAME1STATICTEXT2, wxID_FRAME1STATICTEXT3, wxID_FRAME1STATICTEXT4,
  17. wxID_FRAME1STATICTEXT5, wxID_FRAME1TEXTCTRL1, wxID_FRAME1TEXTCTRL2,
  18. ] = [wx.NewId() for _init_ctrls in range(10)]
  19.  
  20. class Frame1(wx.Frame):
  21. def _init_ctrls(self, prnt):
  22. # generated method, don't edit
  23. wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
  24. pos=wx.Point(380, 327), size=wx.Size(510, 275),
  25. style=wx.DEFAULT_FRAME_STYLE, title=u'Temperature converter')
  26. self.SetClientSize(wx.Size(510, 275))
  27.  
  28. self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
  29. pos=wx.Point(0, 0), size=wx.Size(510, 275),
  30. style=wx.TAB_TRAVERSAL)
  31.  
  32. self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
  33. label=u'Please enter a temperature to convert',
  34. name='staticText1', parent=self.panel1, pos=wx.Point(40, 70),
  35. size=wx.Size(246, 17), style=0)
  36.  
  37. self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
  38. parent=self.panel1, pos=wx.Point(296, 64), size=wx.Size(80, 27),
  39. style=0, value=u'')
  40.  
  41. self.staticText2 = wx.StaticText(id=wxID_FRAME1STATICTEXT2,
  42. label=u'Convert to degrees:', name='staticText2',
  43. parent=self.panel1, pos=wx.Point(161, 143), size=wx.Size(127, 17),
  44. style=0)
  45.  
  46. self.choice1 = wx.Choice(choices=['Centigrade', 'Fahrenheit'],
  47. id=wxID_FRAME1CHOICE1, name='choice1', parent=self.panel1,
  48. pos=wx.Point(296, 136), size=wx.Size(152, 32), style=0)
  49. self.choice1.SetSelection(1)
  50. self.choice1.SetStringSelection(u'Centigrade, Fahrenheit')
  51. self.choice1.Bind(wx.EVT_CHOICE, self.OnChoice1Choice,
  52. id=wxID_FRAME1CHOICE1)
  53.  
  54. self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2, name='textCtrl2',
  55. parent=self.panel1, pos=wx.Point(241, 218), size=wx.Size(99, 27),
  56. style=0, value=u'')
  57. self.textCtrl2.SetMinSize(wx.Size(49, 27))
  58.  
  59. self.staticText3 = wx.StaticText(id=wxID_FRAME1STATICTEXT3, label=u'',
  60. name='staticText3', parent=self.panel1, pos=wx.Point(31, 224),
  61. size=wx.Size(45, 17), style=0)
  62. self.staticText3.SetMinSize(wx.Size(40, 17))
  63.  
  64. self.staticText5 = wx.StaticText(id=wxID_FRAME1STATICTEXT5, label=u'',
  65. name='staticText5', parent=self.panel1, pos=wx.Point(90, 223),
  66. size=wx.Size(145, 17), style=0)
  67.  
  68. self.staticText4 = wx.StaticText(id=wxID_FRAME1STATICTEXT4, label=u'',
  69. name='staticText4', parent=self.panel1, pos=wx.Point(351, 220),
  70. size=wx.Size(68, 17), style=0)
  71.  
  72. def __init__(self, parent):
  73. self._init_ctrls(parent)
  74.  
  75.  
  76.  
  77.  
  78. def OnChoice1Choice(self, event):
  79. x = self.textCtrl1.GetValue()
  80. self.staticText3.SetLabel(x)
  81. x = float(x) #convert to float
  82. ans = self.choice1.GetCurrentSelection()
  83. if ans == 0: #first item in the list
  84. z = Convert_to_C(x)
  85. self.staticText5.SetLabel('degrees Fahrenheit = ')
  86. self.staticText4.SetLabel('degrees Centigrade')
  87. if ans == 1: #second item in the list
  88. z = Convert_to_F(x)
  89. self.staticText5.SetLabel('degrees Centigrade = ')
  90. self.staticText4.SetLabel('degrees Fahrenheit')
  91. z = str(z) #change to a string
  92. self.textCtrl2.SetValue(z)
  93. #event.Skip()
  94. #.............................................................................
  95.  
  96. def create(parent):
  97. return Frame1(parent)
  98.  
  99. class BoaApp(wx.App):
  100. def OnInit(self):
  101. wx.InitAllImageHandlers()
  102. self.main = create(None)
  103. self.main.Show()
  104. self.SetTopWindow(self.main)
  105. return True
  106.  
  107. def main():
  108. application = BoaApp(0)
  109. application.MainLoop()
  110.  
  111. if __name__ == '__main__':
  112. main()
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 3:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC