944,093 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 1646
  • Python RSS
0

YAC wxPython Tiny Calculator

by on Jul 8th, 2006
Yet Another Calculator using wxPython, smaller, but more advanced than the previous one. Wrote this one to test the Curry class, but it didn't work well. So there is no curry there. I found out that one has to be careful with the eval() function. If you write 012 instead of 12, it looks at 012 as an octal number with a denary value of 10. A few safeguards are included.

As you look at the code, you will see that most of it is simple copy and paste with minor modifications. I have added a power button, so you can do square roots like 25**0.5, which should give you a 5.0. Well, enjoy what's there, and improve on it!
Python Code Snippet (Toggle Plain Text)
  1. # yet another calculator, a wxPython tiny calculator that is ...
  2. # tested with Python24 vegaseat 08jul2006
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. """make a frame, inherits wx.Frame"""
  8. def __init__(self):
  9. # create a frame/window, no parent, default to wxID_ANY or -1
  10. wx.Frame.__init__(self, None, wx.ID_ANY, 'wxCalc', pos=(300, 150), size=(185, 200))
  11. self.SetBackgroundColour('green')
  12.  
  13. self.edit1 = wx.TextCtrl(self, -1, value="", pos=(5,5), size=(165, 25))
  14.  
  15. # now create all the buttons and bind the event
  16. self.btn_0 = wx.Button(self, -1, label='0', pos=(5, 130), size=(20, 20))
  17. self.btn_0.Bind(wx.EVT_BUTTON, self.btn_0Click)
  18.  
  19. self.btn_p = wx.Button(self, -1, label='.', pos=(35, 130), size=(20, 20))
  20. self.btn_p.Bind(wx.EVT_BUTTON, self.btn_pClick)
  21.  
  22. self.btn_e = wx.Button(self, -1, label='=', pos=(65, 130), size=(20, 20))
  23. self.btn_e.Bind(wx.EVT_BUTTON, self.btn_eClick)
  24.  
  25. self.btn_a = wx.Button(self, -1, label='+', pos=(105, 130), size=(20, 20))
  26. self.btn_a.Bind(wx.EVT_BUTTON, self.btn_aClick)
  27.  
  28. self.btn_ng = wx.Button(self, -1, label='neg', pos=(135, 130), size=(30, 20))
  29. self.btn_ng.Bind(wx.EVT_BUTTON, self.btn_ngClick)
  30.  
  31. self.btn_1 = wx.Button(self, -1, label='1', pos=(5, 100), size=(20, 20))
  32. self.btn_1.Bind(wx.EVT_BUTTON, self.btn_1Click)
  33.  
  34. self.btn_2 = wx.Button(self, -1, label='2', pos=(35, 100), size=(20, 20))
  35. self.btn_2.Bind(wx.EVT_BUTTON, self.btn_2Click)
  36.  
  37. self.btn_3 = wx.Button(self, -1, label='3', pos=(65, 100), size=(20, 20))
  38. self.btn_3.Bind(wx.EVT_BUTTON, self.btn_3Click)
  39.  
  40. self.btn_s = wx.Button(self, -1, label='-', pos=(105, 100), size=(20, 20))
  41. self.btn_s.Bind(wx.EVT_BUTTON, self.btn_sClick)
  42.  
  43. self.btn_mm = wx.Button(self, -1, label='**', pos=(135, 100), size=(30, 20))
  44. self.btn_mm.Bind(wx.EVT_BUTTON, self.btn_mmClick)
  45. self.btn_mm.SetToolTip(wx.ToolTip('power'))
  46.  
  47. self.btn_4 = wx.Button(self, -1, label='4', pos=(5, 70), size=(20, 20))
  48. self.btn_4.Bind(wx.EVT_BUTTON, self.btn_4Click)
  49.  
  50. self.btn_5 = wx.Button(self, -1, label='5', pos=(35, 70), size=(20, 20))
  51. self.btn_5.Bind(wx.EVT_BUTTON, self.btn_5Click)
  52.  
  53. self.btn_6 = wx.Button(self, -1, label='6', pos=(65, 70), size=(20, 20))
  54. self.btn_6.Bind(wx.EVT_BUTTON, self.btn_6Click)
  55.  
  56. self.btn_m = wx.Button(self, -1, label='*', pos=(105, 70), size=(20, 20))
  57. self.btn_m.Bind(wx.EVT_BUTTON, self.btn_mClick)
  58.  
  59. self.btn_b = wx.Button(self, -1, label='<-', pos=(135, 70), size=(30, 20))
  60. self.btn_b.Bind(wx.EVT_BUTTON, self.btn_bClick)
  61. self.btn_b.SetToolTip(wx.ToolTip('backspace'))
  62.  
  63. self.btn_7 = wx.Button(self, -1, label='7', pos=(5, 40), size=(20, 20))
  64. self.btn_7.Bind(wx.EVT_BUTTON, self.btn_7Click)
  65.  
  66. self.btn_8 = wx.Button(self, -1, label='8', pos=(35, 40), size=(20, 20))
  67. self.btn_8.Bind(wx.EVT_BUTTON, self.btn_8Click)
  68.  
  69. self.btn_9 = wx.Button(self, -1, label='9', pos=(65, 40), size=(20, 20))
  70. self.btn_9.Bind(wx.EVT_BUTTON, self.btn_9Click)
  71.  
  72. self.btn_d = wx.Button(self, -1, label='/', pos=(105, 40), size=(20, 20))
  73. self.btn_d.Bind(wx.EVT_BUTTON, self.btn_dClick)
  74.  
  75. self.btn_c = wx.Button(self, -1, label='c', pos=(135, 40), size=(20, 20))
  76. self.btn_c.Bind(wx.EVT_BUTTON, self.btn_cClick)
  77. self.btn_c.SetToolTip(wx.ToolTip('clear entry'))
  78.  
  79. # show the frame
  80. self.Show(True)
  81.  
  82. def btn_0Click(self, event):
  83. self.edit1.SetValue(self.edit1.GetValue() + '0')
  84.  
  85. def btn_pClick(self, event):
  86. self.edit1.SetValue(self.edit1.GetValue() + '.')
  87.  
  88. def btn_eClick(self, event):
  89. """equal"""
  90. str1 = self.edit1.GetValue()
  91. while str1[0] == '0':
  92. # avoid leading zero (octal) error with eval()
  93. str1 = str1[1:]
  94. if '/' in str1 and '.' not in str1:
  95. # turn into floating point division
  96. str1 = str1 + '.0'
  97. try:
  98. self.edit1.SetValue(str(eval(str1)))
  99. except ZeroDivisionError:
  100. self.edit1.SetValue('division by zero error')
  101.  
  102. def btn_aClick(self, event):
  103. self.edit1.SetValue(self.edit1.GetValue() + '+')
  104.  
  105. def btn_ngClick(self, event):
  106. self.edit1.SetValue('-' + self.edit1.GetValue())
  107.  
  108. def btn_1Click(self, event):
  109. self.edit1.SetValue(self.edit1.GetValue() + '1')
  110.  
  111. def btn_2Click(self, event):
  112. self.edit1.SetValue(self.edit1.GetValue() + '2')
  113.  
  114. def btn_3Click(self, event):
  115. self.edit1.SetValue(self.edit1.GetValue() + '3')
  116.  
  117. def btn_sClick(self, event):
  118. self.edit1.SetValue(self.edit1.GetValue() + '-')
  119.  
  120. def btn_mmClick(self, event):
  121. """power"""
  122. self.edit1.SetValue(self.edit1.GetValue() + '**')
  123.  
  124. def btn_4Click(self, event):
  125. self.edit1.SetValue(self.edit1.GetValue() + '4')
  126.  
  127. def btn_5Click(self, event):
  128. self.edit1.SetValue(self.edit1.GetValue() + '5')
  129.  
  130. def btn_6Click(self, event):
  131. self.edit1.SetValue(self.edit1.GetValue() + '6')
  132.  
  133. def btn_mClick(self, event):
  134. self.edit1.SetValue(self.edit1.GetValue() + '*')
  135.  
  136. def btn_7Click(self, event):
  137. self.edit1.SetValue(self.edit1.GetValue() + '7')
  138.  
  139. def btn_8Click(self, event):
  140. self.edit1.SetValue(self.edit1.GetValue() + '8')
  141.  
  142. def btn_9Click(self, event):
  143. self.edit1.SetValue(self.edit1.GetValue() + '9')
  144.  
  145. def btn_dClick(self, event):
  146. self.edit1.SetValue(self.edit1.GetValue() + '/')
  147.  
  148. def btn_cClick(self, event):
  149. """clear"""
  150. self.edit1.SetValue('')
  151.  
  152. def btn_bClick(self, event):
  153. """backspace"""
  154. self.edit1.SetValue(self.edit1.GetValue()[:-1])
  155.  
  156.  
  157. application = wx.PySimpleApp()
  158. # call class MyFrame
  159. window = MyFrame()
  160. # start the event loop
  161. application.MainLoop()
Message:
Previous Thread in Python Forum Timeline: How to set default directory for saving files in Python IDLE
Next Thread in Python Forum Timeline: Manipulating a list to generate multiple lists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC