電卓プログラム(1)
べただけど初心に帰って電卓を作成した。最小限の機能しかまだないがとりあえず動く。
- 使い方
- Calculator.pywをダブルクリック
- 数字を入力(マウスでボタンを押しても直接入力してもよい)
- ボタンで演算記号を入力(+, -, *, /)
- 続けて数字を入力
- 続いて演算記号か"="ボタンを押す
- 結果が表示される
- 課題
- 小数点数の入力ができない
以下、ソースCalculator.pyw
# coding: utf-8 # ------------------------------------------------------------------- # Calculator ver.1.0 (2008/12/19) # # ------------------------------------------------------------------- import wx import os class MainFrame(wx.Frame): def __init__(self, id, title): width, height = 250, 300 wx.Frame.__init__(self, id, title=u"Calculator", size=wx.Size(width, height)) self.current = " " self.sb = self.CreateStatusBar(2) self.sb.SetStatusWidths([-1, 150]) self.sb.SetStatusText(self.current) self.Btns = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', '=', 'Clear'] self.Btn = list() self.Numbers = list() self.Signs = list() self.Count = 0 HSizer1 = wx.BoxSizer(wx.HORIZONTAL) HSizer2 = wx.BoxSizer(wx.HORIZONTAL) HSizer3 = wx.BoxSizer(wx.HORIZONTAL) HSizer4 = wx.BoxSizer(wx.HORIZONTAL) HSizer5 = wx.BoxSizer(wx.HORIZONTAL) HSizer6 = wx.BoxSizer(wx.HORIZONTAL) HSizer7 = wx.BoxSizer(wx.HORIZONTAL) VSizer = wx.BoxSizer(wx.VERTICAL) Pan = wx.Panel(self, -1) self.TxtCtl1 = wx.TextCtrl(Pan, -1, "", size=(235,-1), style=wx.TE_RIGHT) for i in xrange(len(self.Btns)): self.Btn.append(wx.Button(Pan, -1, self.Btns[i])) self.Btn[0].Bind(wx.EVT_BUTTON, self.PlotNum0) self.Btn[1].Bind(wx.EVT_BUTTON, self.PlotNum1) self.Btn[2].Bind(wx.EVT_BUTTON, self.PlotNum2) self.Btn[3].Bind(wx.EVT_BUTTON, self.PlotNum3) self.Btn[4].Bind(wx.EVT_BUTTON, self.PlotNum4) self.Btn[5].Bind(wx.EVT_BUTTON, self.PlotNum5) self.Btn[6].Bind(wx.EVT_BUTTON, self.PlotNum6) self.Btn[7].Bind(wx.EVT_BUTTON, self.PlotNum7) self.Btn[8].Bind(wx.EVT_BUTTON, self.PlotNum8) self.Btn[9].Bind(wx.EVT_BUTTON, self.PlotNum9) self.Btn[10].Bind(wx.EVT_BUTTON, self.PlotNum10) self.Btn[11].Bind(wx.EVT_BUTTON, self.PlotNum11) self.Btn[12].Bind(wx.EVT_BUTTON, self.PlotNum12) self.Btn[13].Bind(wx.EVT_BUTTON, self.PlotNum13) self.Btn[14].Bind(wx.EVT_BUTTON, self.PlotNum14) self.Btn[15].Bind(wx.EVT_BUTTON, self.PlotNum15) HSizer2.Add(self.Btn[1], 0, wx.ALL, 2) HSizer2.Add(self.Btn[2], 0, wx.ALL, 2) HSizer2.Add(self.Btn[3], 0, wx.ALL, 2) HSizer3.Add(self.Btn[4], 0, wx.ALL, 2) HSizer3.Add(self.Btn[5], 0, wx.ALL, 2) HSizer3.Add(self.Btn[6], 0, wx.ALL, 2) HSizer4.Add(self.Btn[7], 0, wx.ALL, 2) HSizer4.Add(self.Btn[8], 0, wx.ALL, 2) HSizer4.Add(self.Btn[9], 0, wx.ALL, 2) HSizer5.Add(self.Btn[0], 0, wx.ALL, 2) HSizer6.Add(self.Btn[10], 0, wx.ALL, 2) HSizer6.Add(self.Btn[11], 0, wx.ALL, 2) HSizer6.Add(self.Btn[12], 0, wx.ALL, 2) HSizer7.Add(self.Btn[13], 0, wx.ALL, 2) HSizer7.Add(self.Btn[15], 0, wx.ALL, 2) HSizer7.Add(self.Btn[14], 0, wx.ALL, 2) VSizer.Add(self.TxtCtl1, 0, wx.ALL, 2) VSizer.Add(HSizer2, 0, wx.ALL, 2) VSizer.Add(HSizer3, 0, wx.ALL, 2) VSizer.Add(HSizer4, 0, wx.ALL, 2) VSizer.Add(HSizer5, 0, wx.ALL, 2) VSizer.Add(HSizer6, 0, wx.ALL, 2) VSizer.Add(HSizer7, 0, wx.ALL, 2) VSizer.Add(HSizer1, 0, wx.ALL, 2) Pan.SetSizer(VSizer) VSizer.Fit(self) def PlotNum0(self, event): self.TxtCtl1.AppendText('0') def PlotNum1(self, event): self.TxtCtl1.AppendText('1') def PlotNum2(self, event): self.TxtCtl1.AppendText('2') def PlotNum3(self, event): self.TxtCtl1.AppendText('3') def PlotNum4(self, event): self.TxtCtl1.AppendText('4') def PlotNum5(self, event): self.TxtCtl1.AppendText('5') def PlotNum6(self, event): self.TxtCtl1.AppendText('6') def PlotNum7(self, event): self.TxtCtl1.AppendText('7') def PlotNum8(self, event): self.TxtCtl1.AppendText('8') def PlotNum9(self, event): self.TxtCtl1.AppendText('9') def PlotNum10(self, event): self.Numbers.append(self.TxtCtl1.GetValue()+'.') self.Signs.append('+') self.Count += 1 self.TxtCtl1.SetValue("") def PlotNum11(self, event): self.Numbers.append(self.TxtCtl1.GetValue()+'.') self.Signs.append('-') self.Count += 1 self.TxtCtl1.SetValue("") def PlotNum12(self, event): self.Numbers.append(self.TxtCtl1.GetValue()+'.') self.Signs.append('*') self.Count += 1 self.TxtCtl1.SetValue("") def PlotNum13(self, event): self.Numbers.append(self.TxtCtl1.GetValue()+'.') self.Signs.append('/') self.Count += 1 self.TxtCtl1.SetValue("") def PlotNum14(self, event): """ = """ eqn = "" self.Numbers.append(self.TxtCtl1.GetValue()) eqn += self.Numbers[0] for i in xrange(self.Count): eqn += self.Signs[i] eqn += self.Numbers[i+1] self.TxtCtl1.SetValue("") self.TxtCtl1.SetValue(str(eval(eqn))) """ clear variables """ self.Numbers = [] self.Signs = [] self.Count = 0 eqn = "" def PlotNum15(self, event): """ clear """ self.TxtCtl1.SetValue("") class Application(wx.App): def OnInit(self): frame = MainFrame(None, -1) frame.Show(True) self.SetTopWindow(frame) return True def main(): import time app = Application(0) print u"info> 起動時刻: %s." % time.ctime(time.time()) appl_boot_path = os.getcwd() print u"info> 起動ディレクトリ: %s." % appl_boot_path app.MainLoop() if __name__ == '__main__': main() #eof