1. 程式人生 > >wxPython+Python3+eval實現基本運算和高階運算計算器(School Project)

wxPython+Python3+eval實現基本運算和高階運算計算器(School Project)

這其實是我寫的第一次Python圖形化介面程式,當時還不會用Qt,於是就用wxPython寫的,wx相對於Qt來說還是要麻煩一些,介面看上去也沒有Qt高階。博主女生,嘗試把計算器介面換成粉紅色淡藍色,最後發現還是黑灰白的順眼一點。

執行介面如下:

在這裡插入圖片描述

下面是原始碼:


# -*- coding: utf-8 -*-
import wx
import math

class MyCalculator(wx.Frame):
    def __init__(self):
        super().__init__(parent=None,title="MyCalculator",size=(480
,400),style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX)) self.Center() panel = wx.Panel(parent=self) panel.SetBackgroundColour('white') panel.Center() font = wx.Font(14, wx.ROMAN, wx.NORMAL, wx.BOLD, underline=False) filemenu =
wx.Menu() menuAbout = filemenu.Append(wx.ID_ABOUT,"&About") filemenu.AppendSeparator() menuExit = filemenu.Append(wx.ID_EXIT,"&Exit") menuBar = wx.MenuBar() menuBar.Append(filemenu, "&File") self.SetMenuBar(menuBar) menuBar.SetFont(
font) self.Bind(wx.EVT_MENU, self.OnAbout,menuAbout) self.Bind(wx.EVT_MENU, self.OnExit,menuExit) self.equation = "" self.result = 0 self.ResultFlag = 0 self.textprint = wx.TextCtrl(panel,-1,'',style=wx.TE_MULTILINE) self.textprint.SetFont(font) self.textprint.BackgroundColour = 'white' boxsize = wx.BoxSizer(wx.VERTICAL) boxsize.Add(self.textprint,1,wx.EXPAND) gridsizer = wx.GridSizer(cols=5,rows=6,vgap=1,hgap=0) self.btn0 = wx.Button(panel, label='0') self.btn1 = wx.Button(panel, label='1') self.btn2 = wx.Button(panel, label='2') self.btn3 = wx.Button(panel, label='3') self.btn4 = wx.Button(panel, label='4') self.btn5 = wx.Button(panel, label='5') self.btn6 = wx.Button(panel, label='6') self.btn7 = wx.Button(panel, label='7') self.btn8 = wx.Button(panel, label='8') self.btn9 = wx.Button(panel, label='9') self.btn10 = wx.Button(panel, label='^') self.btn11 = wx.Button(panel, label='.') self.btn12 = wx.Button(panel, label='=') self.btn13 = wx.Button(panel, label='÷') self.btn14 = wx.Button(panel, label='×') self.btn15 = wx.Button(panel, label='-') self.btn16 = wx.Button(panel, label='+') self.btn17 = wx.Button(panel, label='Ans') self.btn18 = wx.Button(panel, label='AC') self.btn19 = wx.Button(panel, label='DEL') self.btn20 = wx.Button(panel, label='sin') self.btn21 = wx.Button(panel, label='cos') self.btn22 = wx.Button(panel, label='tan') self.btn23 = wx.Button(panel, label='(') self.btn24 = wx.Button(panel, label=')') self.btn25 = wx.Button(panel, label='π') self.btn26 = wx.Button(panel, label='log') self.btn27 = wx.Button(panel, label='ln') self.btn28 = wx.Button(panel, label='sqrt') self.btn29 = wx.Button(panel, label='mod') self.btn0.SetFont(font) self.btn1.SetFont(font) self.btn2.SetFont(font) self.btn3.SetFont(font) self.btn4.SetFont(font) self.btn5.SetFont(font) self.btn6.SetFont(font) self.btn7.SetFont(font) self.btn8.SetFont(font) self.btn9.SetFont(font) self.btn10.SetFont(font) self.btn11.SetFont(font) self.btn12.SetFont(font) self.btn13.SetFont(font) self.btn14.SetFont(font) self.btn15.SetFont(font) self.btn16.SetFont(font) self.btn17.SetFont(font) self.btn18.SetFont(font) self.btn19.SetFont(font) self.btn20.SetFont(font) self.btn21.SetFont(font) self.btn22.SetFont(font) self.btn23.SetFont(font) self.btn24.SetFont(font) self.btn25.SetFont(font) self.btn26.SetFont(font) self.btn27.SetFont(font) self.btn28.SetFont(font) self.btn29.SetFont(font) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn0) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn1) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn2) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn3) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn4) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn5) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn6) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn7) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn8) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn9) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn10) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn11) self.Bind(wx.EVT_BUTTON,self.OnResult,self.btn12) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn13) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn14) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn15) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn16) self.Bind(wx.EVT_BUTTON,self.OnAns,self.btn17) self.Bind(wx.EVT_BUTTON,self.OnAc,self.btn18) self.Bind(wx.EVT_BUTTON,self.OnDel,self.btn19) self.Bind(wx.EVT_BUTTON,self.OnOp,self.btn20) self.Bind(wx.EVT_BUTTON,self.OnOp,self.btn21) self.Bind(wx.EVT_BUTTON,self.OnOp,self.btn22) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn23) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn24) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn25) self.Bind(wx.EVT_BUTTON,self.OnOp,self.btn26) self.Bind(wx.EVT_BUTTON,self.OnOp,self.btn27) self.Bind(wx.EVT_BUTTON,self.OnOp,self.btn28) self.Bind(wx.EVT_BUTTON,self.OnAppend,self.btn29) self.btn0.BackgroundColour = 'white' self.btn1.BackgroundColour = 'white' self.btn2.BackgroundColour = 'white' self.btn3.BackgroundColour = 'white' self.btn4.BackgroundColour = 'white' self.btn5.BackgroundColour = 'white' self.btn6.BackgroundColour = 'white' self.btn7.BackgroundColour = 'white' self.btn8.BackgroundColour = 'white' self.btn9.BackgroundColour = 'white' gridsizer.AddMany([(self.btn18, 0, wx.EXPAND), (self.btn10, 0, wx.EXPAND), (self.btn28, 0, wx.EXPAND), (self.btn29, 0, wx.EXPAND), (self.btn19, 0, wx.EXPAND), (self.btn26, 0, wx.EXPAND), (self.btn23, 0, wx.EXPAND), (self.btn24, 0, wx.EXPAND), (self.btn27, 0, wx.EXPAND), (self.btn13, 0, wx.EXPAND), (self.btn20, 0, wx.EXPAND), (self.btn7, 0, wx.EXPAND), (self.btn8, 0, wx.EXPAND), (self.btn9, 0, wx.EXPAND), (self.btn14, 0, wx.EXPAND), (self.btn21, 0, wx.EXPAND), (self.btn4,0, wx.EXPAND), (self.btn5, 0, wx.EXPAND), (self.btn6, 0, wx.EXPAND), (self.btn15, 0, wx.EXPAND), (self.btn22, 0, wx.EXPAND), (self.btn1, 0, wx.EXPAND), (self.btn2, 0, wx.EXPAND), (self.btn3, 0, wx.EXPAND), (self.btn16, 0, wx.EXPAND), (self.btn25, 0, wx.EXPAND), (self.btn17, 0, wx.EXPAND), (self.btn0, 0, wx.EXPAND), (self.btn11, 0, wx.EXPAND), (self.btn12, 0, wx.EXPAND),]) boxsize.Add(gridsizer,6,wx.EXPAND) panel.SetSizerAndFit(boxsize) self.textprint.SetValue(self.equation) def OnAbout(self,event): dlg = wx.MessageDialog(self,"This is a calculator written by Python3.6.","About Program",wx.OK) dlg.ShowModal() dlg.Destroy() def OnAppend(self,event): ops = ['×', '÷', '+', '-', '^','mod','.'] eventbutton = event.GetEventObject() label = eventbutton.GetLabel() if label not in ops and self.ResultFlag != 0: self.ResultFlag = 0 self.textprint.Clear() self.result = 0 self.equation = "" if label in ops and self.ResultFlag != 0: self.equation = str(self.result) self.ResultFlag = 0 self.equation += label self.textprint.SetValue(self.equation) def OnOp(self,event): label = event.GetEventObject().GetLabel() +'(' if self.ResultFlag != 0: self.equation = label + str(self.result) self.ResultFlag = 0 else: self.equation += label self.textprint.SetValue(self.equation) def OnDel(self,event): if '=' not in self.equation: self.ResultFlag = 0 self.result = 0 self.equation = self.equation[:-1] self.textprint.SetValue(self.equation) def OnAc(self,event): self.textprint.Clear() self.result = 0 self.ResultFlag = 0 self.equation = "" def OnExit(self,event): self.Close(True) def OnResult(self,event): string = self.equation if 'log' in string: string = string.replace('log','log10') MathFac = ['sqrt','sin','cos','tan','log10'] for fac in MathFac: if fac in string: string = string.replace(fac,'math.'+fac) if 'ln' in string: string = string.replace('ln','math.log') if '^' in string: string = string.replace('^','**') if '×' in string: string = string.replace('×','*') if '÷' in string: string = string.replace('÷','/') if 'π' in string: string = string.replace('π','math.pi') if 'mod' in string: string = string.replace('mod','%') count1 = 0 count2 = 0 for i in string: if i == '(': count1 += 1 string += ')' for i in string: if i == ')': count2 += 1 if count1 < count2: for i in range(count2-count1): string = '(' +string count1 = 0 count2 = 0 try: self.result = round(eval(string),10) self.equa