Python实现的科学计算器功能示例

本文实例讲述了Python实现的科学计算器功能。分享给大家供大家参考,具体如下:

import wx
import re
import math
# begin wxGlade: extracode
# end wxGlade
ans=0
ts=""
class MyFrame(wx.Frame):
  def __init__(self, *args, **kwds):
    # begin wxGlade: MyFrame.__init__
    kwds["style"] = wx.DEFAULT_FRAME_STYLE
    wx.Frame.__init__(self, *args, **kwds)
    self.text_ctrl_1 = wx.TextCtrl(self, -1, "" ,style=wx.TE_READONLY)
    self.text_ctrl_2 = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY)
    self.button_37 = wx.Button(self, -1, "7")
    self.button_38 = wx.Button(self, -1, "8")
    self.button_39 = wx.Button(self, -1, "9")
    self.button_40 = wx.Button(self, -1, "+")
    self.button_41 = wx.Button(self, -1, "-")
    self.button_42 = wx.Button(self, -1, "4")
    self.button_43 = wx.Button(self, -1, "5")
    self.button_44 = wx.Button(self, -1, "6")
    self.button_45 = wx.Button(self, -1, "x")
    self.button_46 = wx.Button(self, -1, "/")
    self.button_47 = wx.Button(self, -1, "1")
    self.button_48 = wx.Button(self, -1, "2")
    self.button_49 = wx.Button(self, -1, "3")
    self.button_50 = wx.Button(self, -1, "(")
    self.button_51 = wx.Button(self, -1, ")")
    self.button_52 = wx.Button(self, -1, "0")
    self.button_53 = wx.Button(self, -1, ".")
    self.button_54 = wx.Button(self, -1, "ans")
    self.button_55 = wx.Button(self, -1, "clear")
    self.button_56 = wx.Button(self, -1, "=")
    self.button_57 = wx.Button(self, -1, "pi")
    self.button_58 = wx.Button(self, -1, "e")
    self.button_59 = wx.Button(self, -1, "1/x")
    self.button_60 = wx.Button(self, -1, "x^2")
    self.button_61 = wx.Button(self, -1, "x^y")
    self.button_62 = wx.Button(self, -1, "sqrt")
    self.button_63 = wx.Button(self, -1, "sin")
    self.button_64 = wx.Button(self, -1, "cos")
    self.button_65 = wx.Button(self, -1, "tan")
    self.button_66 = wx.Button(self, -1, "log")
    self.button_67 = wx.Button(self, -1, "ln")
    self.button_68 = wx.Button(self, -1, "n!")
    self.button_69 = wx.Button(self, -1, "mod")
    self.button_70 = wx.Button(self, -1, "int")
    self.button_71 = wx.Button(self, -1, "yu")
    self.button_72 = wx.Button(self, -1, "|")
    self.button_73 = wx.Button(self, -1, "~")
    self.button_74 = wx.Button(self, -1, "xor")
    self.__set_properties()
    self.__do_layout()
    self.Bind(wx.EVT_BUTTON, self.bu37, self.button_37)
    self.Bind(wx.EVT_BUTTON, self.bu38, self.button_38)
    self.Bind(wx.EVT_BUTTON, self.bu39, self.button_39)
    self.Bind(wx.EVT_BUTTON, self.bu40, self.button_40)
    self.Bind(wx.EVT_BUTTON, self.bu41, self.button_41)
    self.Bind(wx.EVT_BUTTON, self.bu42, self.button_42)
    self.Bind(wx.EVT_BUTTON, self.bu43, self.button_43)
    self.Bind(wx.EVT_BUTTON, self.bu44, self.button_44)
    self.Bind(wx.EVT_BUTTON, self.bu45, self.button_45)
    self.Bind(wx.EVT_BUTTON, self.bu46, self.button_46)
    self.Bind(wx.EVT_BUTTON, self.bu47, self.button_47)
    self.Bind(wx.EVT_BUTTON, self.bu48, self.button_48)
    self.Bind(wx.EVT_BUTTON, self.bu49, self.button_49)
    self.Bind(wx.EVT_BUTTON, self.bu50, self.button_50)
    self.Bind(wx.EVT_BUTTON, self.bu51, self.button_51)
    self.Bind(wx.EVT_BUTTON, self.bu52, self.button_52)
    self.Bind(wx.EVT_BUTTON, self.bu53, self.button_53)
    self.Bind(wx.EVT_BUTTON, self.bu54, self.button_54)
    self.Bind(wx.EVT_BUTTON, self.bu55, self.button_55)
    self.Bind(wx.EVT_BUTTON, self.bu56, self.button_56)
    self.Bind(wx.EVT_BUTTON, self.bu57, self.button_57)
    self.Bind(wx.EVT_BUTTON, self.bu58, self.button_58)
    self.Bind(wx.EVT_BUTTON, self.bu59, self.button_59)
    self.Bind(wx.EVT_BUTTON, self.bu60, self.button_60)
    self.Bind(wx.EVT_BUTTON, self.bu61, self.button_61)
    self.Bind(wx.EVT_BUTTON, self.bu62, self.button_62)
    self.Bind(wx.EVT_BUTTON, self.bu63, self.button_63)
    self.Bind(wx.EVT_BUTTON, self.bu64, self.button_64)
    self.Bind(wx.EVT_BUTTON, self.bu65, self.button_65)
    self.Bind(wx.EVT_BUTTON, self.bu66, self.button_66)
    self.Bind(wx.EVT_BUTTON, self.bu67, self.button_67)
    self.Bind(wx.EVT_BUTTON, self.bu68, self.button_68)
    self.Bind(wx.EVT_BUTTON, self.bu69, self.button_69)
    self.Bind(wx.EVT_BUTTON, self.bu70, self.button_70)
    self.Bind(wx.EVT_BUTTON, self.bu71, self.button_71)
    self.Bind(wx.EVT_BUTTON, self.bu72, self.button_72)
    self.Bind(wx.EVT_BUTTON, self.bu73, self.button_73)
    self.Bind(wx.EVT_BUTTON, self.bu74, self.button_74)
    # end wxGlade
    self.Show(True)
  def __set_properties(self):
    # begin wxGlade: MyFrame.__set_properties
    self.SetTitle("Python Calculater by CYG")
    self.text_ctrl_1.SetMinSize((500, 30))
    self.text_ctrl_2.SetMinSize((500, 50))
    self.button_37.SetMinSize((100, 60))
    self.button_38.SetMinSize((100, 60))
    self.button_39.SetMinSize((100, 60))
    self.button_40.SetMinSize((100, 60))
    self.button_41.SetMinSize((100, 60))
    self.button_42.SetMinSize((100, 60))
    self.button_43.SetMinSize((100, 60))
    self.button_44.SetMinSize((100, 60))
    self.button_46.SetMinSize((100, 60))
    self.button_45.SetMinSize((100, 60))
    self.button_47.SetMinSize((100, 60))
    self.button_48.SetMinSize((100, 60))
    self.button_49.SetMinSize((100, 60))
    self.button_50.SetMinSize((100, 60))
    self.button_51.SetMinSize((100, 60))
    self.button_52.SetMinSize((100, 60))
    self.button_53.SetMinSize((100, 60))
    self.button_54.SetMinSize((100, 60))
    self.button_55.SetMinSize((100, 60))
    self.button_56.SetMinSize((100, 60))
    self.button_57.SetMinSize((83, 50))
    self.button_58.SetMinSize((83, 50))
    self.button_59.SetMinSize((83, 50))
    self.button_60.SetMinSize((83, 50))
    self.button_61.SetMinSize((83, 50))
    self.button_62.SetMinSize((83, 50))
    self.button_63.SetMinSize((83, 50))
    self.button_64.SetMinSize((83, 50))
    self.button_65.SetMinSize((83, 50))
    self.button_66.SetMinSize((83, 50))
    self.button_67.SetMinSize((83, 50))
    self.button_68.SetMinSize((83, 50))
    self.button_69.SetMinSize((83, 50))
    self.button_70.SetMinSize((83, 50))
    self.button_71.SetMinSize((83, 50))
    self.button_72.SetMinSize((83, 50))
    self.button_73.SetMinSize((83, 50))
    self.button_74.SetMinSize((83, 50))
    # end wxGlade
  def __do_layout(self):
    # begin wxGlade: MyFrame.__do_layout
    sizer_2 = wx.BoxSizer(wx.VERTICAL)
    sizer_3 = wx.BoxSizer(wx.VERTICAL)
    grid_sizer_1 = wx.GridSizer(4, 5, 0, 0)
    grid_sizer_2 = wx.GridSizer(3, 6, 0, 0)
    sizer_3.Add(self.text_ctrl_2, 0, 0, 0)
    sizer_3.Add(self.text_ctrl_1, 0, 0, 0)
    grid_sizer_1.Add(self.button_37, 0, 0, 0)
    grid_sizer_1.Add(self.button_38, 0, 0, 0)
    grid_sizer_1.Add(self.button_39, 0, 0, 0)
    grid_sizer_1.Add(self.button_40, 0, 0, 0)
    grid_sizer_1.Add(self.button_41, 0, 0, 0)
    grid_sizer_1.Add(self.button_42, 0, 0, 0)
    grid_sizer_1.Add(self.button_43, 0, 0, 0)
    grid_sizer_1.Add(self.button_44, 0, 0, 0)
    grid_sizer_1.Add(self.button_45, 0, 0, 0)
    grid_sizer_1.Add(self.button_46, 0, 0, 0)
    grid_sizer_1.Add(self.button_47, 0, 0, 0)
    grid_sizer_1.Add(self.button_48, 0, 0, 0)
    grid_sizer_1.Add(self.button_49, 0, 0, 0)
    grid_sizer_1.Add(self.button_50, 0, 0, 0)
    grid_sizer_1.Add(self.button_51, 0, 0, 0)
    grid_sizer_1.Add(self.button_52, 0, 0, 0)
    grid_sizer_1.Add(self.button_53, 0, 0, 0)
    grid_sizer_1.Add(self.button_54, 0, 0, 0)
    grid_sizer_1.Add(self.button_55, 0, 0, 0)
    grid_sizer_1.Add(self.button_56, 0, 0, 0)
    grid_sizer_2.Add(self.button_57, 0, 0, 0)
    grid_sizer_2.Add(self.button_58, 0, 0, 0)
    grid_sizer_2.Add(self.button_59, 0, 0, 0)
    grid_sizer_2.Add(self.button_60, 0, 0, 0)
    grid_sizer_2.Add(self.button_61, 0, 0, 0)
    grid_sizer_2.Add(self.button_62, 0, 0, 0)
    grid_sizer_2.Add(self.button_63, 0, 0, 0)
    grid_sizer_2.Add(self.button_64, 0, 0, 0)
    grid_sizer_2.Add(self.button_65, 0, 0, 0)
    grid_sizer_2.Add(self.button_66, 0, 0, 0)
    grid_sizer_2.Add(self.button_67, 0, 0, 0)
    grid_sizer_2.Add(self.button_68, 0, 0, 0)
    grid_sizer_2.Add(self.button_69, 0, 0, 0)
    grid_sizer_2.Add(self.button_70, 0, 0, 0)
    grid_sizer_2.Add(self.button_71, 0, 0, 0)
    grid_sizer_2.Add(self.button_72, 0, 0, 0)
    grid_sizer_2.Add(self.button_73, 0, 0, 0)
    grid_sizer_2.Add(self.button_74, 0, 0, 0)
    sizer_3.Add(grid_sizer_1, 1, wx.EXPAND, 0)
    sizer_3.Add(grid_sizer_2, 1, wx.EXPAND, 0)
    sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
    self.SetSizer(sizer_2)
    sizer_2.Fit(self)
    self.Layout()
    # end wxGlade
  def bu37(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts+="7"
    self.text_ctrl_1.AppendText("7")
    event.Skip()
  def bu38(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts+="8"
    self.text_ctrl_1.AppendText("8")
    event.Skip()
  def bu39(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "9"
    self.text_ctrl_1.AppendText("9")
    event.Skip()
  def bu40(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "+"
    self.text_ctrl_1.AppendText("+")
    event.Skip()
  def bu41(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "-"
    self.text_ctrl_1.AppendText("-")
    event.Skip()
  def bu42(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "4"
    self.text_ctrl_1.AppendText("4")
    event.Skip()
  def bu43(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "5"
    self.text_ctrl_1.AppendText("5")
    event.Skip()
  def bu44(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "6"
    self.text_ctrl_1.AppendText("6")
    event.Skip()
  def bu45(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "*"
    self.text_ctrl_1.AppendText("*")
    event.Skip()
  def bu46(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "/"
    self.text_ctrl_1.AppendText("/")
    event.Skip()
  def bu47(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "1"
    self.text_ctrl_1.AppendText("1")
    event.Skip()
  def bu48(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "2"
    self.text_ctrl_1.AppendText("2")
    event.Skip()
  def bu49(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "3"
    self.text_ctrl_1.AppendText("3")
    event.Skip()
  def bu50(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "("
    self.text_ctrl_1.AppendText("(")
    event.Skip()
  def bu51(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += ")"
    self.text_ctrl_1.AppendText(")")
    event.Skip()
  def bu52(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "0"
    self.text_ctrl_1.AppendText("0")
    event.Skip()
  def bu53(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "."
    self.text_ctrl_1.AppendText(".")
    event.Skip()
  def bu54(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "ans"
    self.text_ctrl_1.AppendText("ans")
    event.Skip()
  def bu55(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    self.text_ctrl_1.Clear()
    self.text_ctrl_2.Clear()
    ans=0
    ts=""
    event.Skip()
  def minus_operation(self,expresstion):
    minus_operators = re.split("-", expresstion)
    calc_list = re.findall("[0-9]", expresstion)
    if minus_operators[0] == "":
      calc_list[0] = '-%s' % calc_list[0]
    res = reduce(lambda x, y: float(x) - float(y), calc_list)
    return res
  def del_duplicates(self,ts):
    ts = ts.replace("++", "+")
    ts = ts.replace("--", "-")
    ts = ts.replace("+-", "-")
    ts = ts.replace("--", "+")
    ts = ts.replace('- -', "+")
    return ts
  def mutiply_dividend(self,expresstion):
    calc_list = re.split("[*/]", expresstion)
    operators = re.findall("[*/]", expresstion)
    res = None
    for index, i in enumerate(calc_list):
      if res:
        if operators[index - 1] == '*':
          res *= float(i)
        elif operators[index - 1] == '/':
          res /= float(i)
      else:
        res = float(i)
    return res
  def special_features(self,plus_and_minus_operators, multiply_and_dividend):
    for index, i in enumerate(multiply_and_dividend):
      i = i.strip()
      if i.endswith("*") or i.endswith("/"):
        multiply_and_dividend[index] = multiply_and_dividend[index] + plus_and_minus_operators[index] + \
                        multiply_and_dividend[index + 1]
        del multiply_and_dividend[index + 1]
        del plus_and_minus_operators[index]
    return plus_and_minus_operators, multiply_and_dividend
  def minus_special(self,operator_list, calc_list):
    for index, i in enumerate(calc_list):
      if i == '':
        calc_list[index + 1] = i + calc_list[index + 1].strip()
  def figure_up(self,ts):
    ts = ts.strip("()")
    ts = self.del_duplicates(ts)
    plus_and_minus_operators = re.findall("[+-]", ts)
    multiply_and_dividend = re.split("[+-]", ts)
    if len(multiply_and_dividend[0].strip()) == 0:
      multiply_and_dividend[1] = plus_and_minus_operators[0] + multiply_and_dividend[1]
      del multiply_and_dividend[0]
      del plus_and_minus_operators[0]
    plus_and_minus_operators, multiply_and_dividend = self.special_features(plus_and_minus_operators,
                                      multiply_and_dividend)
    for index, i in enumerate(multiply_and_dividend):
      if re.search("[*/]", i):
        sub_res = self.mutiply_dividend(i)
        multiply_and_dividend[index] = sub_res
    #print(multiply_and_dividend, plus_and_minus_operators)
    final_res = None
    for index, item in enumerate(multiply_and_dividend):
      if final_res:
        if plus_and_minus_operators[index - 1] == '+':
          final_res += float(item)
        elif plus_and_minus_operators[index - 1] == '-':
          final_res -= float(item)
      else:
        final_res = float(item)
    return final_res
  def bu56(self, event): # wxGlade: MyFrame.<event_handler>
    global ans
    global ts
    if re.search("pi", ts):
      lists = re.findall("pi", ts)
      for i in range(0, len(lists)):
        te = str(math.pi)
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("pi", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("e", ts):
      lists = re.findall("e", ts)
      for i in range(0, len(lists)):
        te = str(math.e)
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("e", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("sin.*\)",ts):
      lists=re.findall("sin(.+?)",ts)
      for i in range(0,len(lists)):
        te=float(lists[i])
        te=str(math.sin(te))
        self.text_ctrl_2.SetValue(te)
        ts=re.sub("sin.*?\)",te,ts,1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("cos.*\)", ts):
      lists = re.findall("cos(.+?)", ts)
      for i in range(0, len(lists)):
        te = float(lists[i])
        te = str(math.cos(te))
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("cos.*?\)", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("tan.*\)", ts):
      lists = re.findall("tan(.+?)", ts)
      for i in range(0, len(lists)):
        te = float(lists[i])
        te = str(math.tan(te))
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("tan.*?\)", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("ln.*\)", ts):
      lists = re.findall("ln(.+?)", ts)
      for i in range(0, len(lists)):
        te = float(lists[i])
        te = str(math.log(te))
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("ln.*?\)", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("log.*\)", ts):
      lists = re.findall("log(.+?)", ts)
      for i in range(0, len(lists)):
        te = float(lists[i])
        te = str(math.log(te)/math.log(10))
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("log.*?\)", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("sqrt.*\)", ts):
      lists = re.findall("sqrt(.+?)", ts)
      for i in range(0, len(lists)):
        te = float(lists[i])
        te = str(math.sqrt(te))
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("sqrt.*?\)", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("int.*\)", ts):
      lists = re.findall("int(.+?)", ts)
      for i in range(0, len(lists)):
        te=float(lists[i])
        te=int(te)
        te = str(te)
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("int.*?\)", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("ans", ts):
      lists = re.findall("ans", ts)
      for i in range(0, len(lists)):
        te = str(ans)
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("ans", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
    if re.search("?−?\d∗\.?\d∗?\^?−?\d∗\.?\d∗?", ts):
      lists1 = re.findall("?(−?\d∗\.?\d∗?)?\^", ts)
      lists2 = re.findall("\^?(−?\d∗\.?\d∗)?", ts)
      for i in range(0, len(lists1)):
        te1=float(lists1[i])
        te2=float(lists2[i])
        #print te1
        #print te2
        te=math.pow(te1,te2)
        te = str(te)
        self.text_ctrl_2.SetValue(te)
        ts = re.sub("?[0−9]∗\.?[0−9]∗??\^?[0−9]∗\.?[0−9]∗?", te, ts, 1)
        #self.text_ctrl_1.SetValue(ts)
      #ts = re.sub("?−?\d∗\.?\d∗?\^?−?\d∗\.?\d∗??", te, ts, 1)
    if re.search("?−?\d∗?\!", ts):
        lists = re.findall("?(−?\d+?)?\!", ts)
        for i in range(0, len(lists)):
          te = float(lists[i])
          te = math.factorial(te)
          te = str(te)
          self.text_ctrl_2.SetValue(te)
          ts = re.sub("?−?\d+??\!", te, ts, 1)
          #self.text_ctrl_1.SetValue(ts)
    #print ts
    flag=True
    while flag:
      m = re.search("[()]∗", ts)
      if m:
        sub_res=self.figure_up(m.group())
        ts=ts.replace(m.group(),str(sub_res))
      else:
        ans=self.figure_up(ts)
        flag=False
    self.text_ctrl_2.SetValue(str(ans))
      #ans=
    #self.text_ctrl_2.SetValue(str(ans))
    # self.text_ctrl_1.Clear()
    event.Skip()
  def bu57(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "pi"
    self.text_ctrl_1.AppendText("pi")
    event.Skip()
  def bu58(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "e"
    self.text_ctrl_1.AppendText("e")
    event.Skip()
  def bu59(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "^(-1)"
    self.text_ctrl_1.AppendText("^(-1)")
    event.Skip()
  def bu60(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "^2"
    self.text_ctrl_1.AppendText("^2")
    event.Skip()
  def bu61(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "^"
    self.text_ctrl_1.AppendText("^")
    event.Skip()
  def bu62(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "sqrt("
    self.text_ctrl_1.AppendText("sqrt(")
    event.Skip()
  def bu63(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "sin("
    self.text_ctrl_1.AppendText("sin(")
    event.Skip()
  def bu64(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "cos("
    self.text_ctrl_1.AppendText("cos(")
    event.Skip()
  def bu65(self, event): # wxGlade: MyFrame# .<event_handler>
    global ts
    ts += "tan("
    self.text_ctrl_1.AppendText("tan(")
    event.Skip()
  def bu66(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "log("
    self.text_ctrl_1.AppendText("log(")
    event.Skip()
  def bu67(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "ln("
    self.text_ctrl_1.AppendText("ln(")
    event.Skip()
  def bu68(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "!"
    self.text_ctrl_1.AppendText("!")
    event.Skip()
  def bu69(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "m"
    self.text_ctrl_1.AppendText("m")#qumo
    event.Skip()
  def bu70(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "int("
    self.text_ctrl_1.AppendText("int(")
    event.Skip()
  def bu71(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "u"
    self.text_ctrl_1.AppendText("u")#yu
    event.Skip()
  def bu72(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "|"
    self.text_ctrl_1.AppendText("|")#huo
    event.Skip()
  def bu73(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "n("
    self.text_ctrl_1.AppendText("n(")#fei
    event.Skip()
  def bu74(self, event): # wxGlade: MyFrame.<event_handler>
    global ts
    ts += "x"
    self.text_ctrl_1.AppendText("x")#yihuo
    event.Skip()
    # end of class MyFrame
if __name__ == "__main__":
  app=wx.App(False)
  myframe= MyFrame(None)
  app.MainLoop()

PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:

在线一元函数(方程)求解计算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科学计算器在线使用_高级计算器在线计算:
http://tools.jb51.net/jisuanqi/jsqkexue

在线计算器_标准计算器:
http://tools.jb51.net/jisuanqi/jsq

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

时间: 2017-08-01

Python科学计算之NumPy入门教程

前言 NumPy是Python用于处理大型矩阵的一个速度极快的数学库.它允许你在Python中做向量和矩阵的运算,而且很多底层的函数都是用C写的,你将获得在普通Python中无法达到的运行速度.这是由于矩阵中每个元素的数据类型都是一样的,这也就减少了运算过程中的类型检测. 矩阵基础 在 numpy 包中我们用数组来表示向量,矩阵和高阶数据结构.他们就由数组构成,一维就用一个数组表示,二维就是数组中包含数组表示. 创建 # coding: utf-8 import numpy as np a =

Python计算斗牛游戏概率算法实例分析

本文实例讲述了Python计算斗牛游戏概率算法.分享给大家供大家参考,具体如下: 过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛.在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率). 斗牛的玩法是: 1. 把牌中的JQK都拿出来 2. 每个人发5张牌 3. 如果5张牌中任意三张加在一起是10的 倍数,就是有牛.剩下两张牌的和的10的余数就是牛数. 牌的大小: 4条 > 3条 > 牛十 > 牛九 > -- >

Python开发的实用计算器完整实例

本文实例讲述了Python开发的实用计算器.分享给大家供大家参考,具体如下: 实现功能:图形界面PyQt,输入框,+,-,*,/ :乘方 ,开方 ,取余,清零. 1. Python代码: #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Author : Mr.LiuYC Created on 2014-09-30 E-Mail : liuyanchen0725@gmail.com Introduction: 简易计算器 实现图形界面PyQt,输

Python设计实现的计算器功能完整实例

本文实例讲述了Python设计实现的计算器功能.分享给大家供大家参考,具体如下: 通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3)/(16-3*2)) 我的处理计算基本思路是: 解题思路是,需要优先处理内层括号运算--外层括号运算--先乘除后加减的原则: 1.正则处理用户输入的字符串,然后对其进行判断,判断计算公式是否有括号,有就先将计算公式进

Python PyQt5实现的简易计算器功能示例

本文实例讲述了Python PyQt5实现的简易计算器功能.分享给大家供大家参考,具体如下: 这里剩下计算函数(self.calculator)未实现,有兴趣的朋友可以实现它 [知识点] 1.利用循环添加按钮部件,及给每个按钮设置信号/槽 2.给按钮设置固定大小:button.setFixedSize(QtCore.QSize(60,30)) 3.取事件的的发送者(此例为各个按钮)的文本: self.sender().text() [效果图] [源代码] import sys from PyQt

python中numpy基础学习及进行数组和矢量计算

前言 在python 中有时候我们用数组操作数据可以极大的提升数据的处理效率,类似于R的向量化操作,是的数据的操作趋于简单化,在python 中是使用numpy模块可以进行数组和矢量计算. 下面来看下简单的例子 import numpy as np data=np.array([2,5,6,8,3]) #构造一个简单的数组 print(data) 结果: [2 5 6 8 3] data1=np.array([[2,5,6,8,3],np.arange(5)]) #构建一个二维数组 print(

Python实现的排列组合计算操作示例

本文实例讲述了Python实现的排列组合计算操作.分享给大家供大家参考,具体如下: 1. 调用 scipy 计算排列组合的具体数值 >> from scipy.special import comb, perm >> perm(3, 2) 6.0 >> comb(3, 2) 3.0 2. 调用 itertools 获取排列组合的全部情况数 >> from itertools import combinations, permutations >>

Python中shape计算矩阵的方法示例

本文实例讲述了Python中shape计算矩阵的方法.分享给大家供大家参考,具体如下: 看到机器学习算法时,注意到了shape计算矩阵的方法接下来就讲讲我的理解吧 >>> from numpy import * >>> import operator >>> a =mat([[1,2,3],[5,6,9]]) >>> a matrix([[1, 2, 3], [5, 6, 9]]) >>> shape(a) (2,

python 计算两个日期相差多少个月实例代码

近期,由于业务需要计算两个日期之前相差多少个月.我在网上找了很久,结果发现万能的python,居然没有一个模块计算两个日期的月数,像Java.C#之类的高级语言,都会有(date1-date2).months的现成方法,觉得不可思议.说句实在的,一直觉得python 的日期处理模块真心不好用. 哦,对了,别跟我说 datetime, calendar, dateutil 这些模块,因为我都试过了,都没用.有个竟然算出来还有错.datetime.timedelta只能计算出日时分秒.对年月却不支持

python 排列组合之itertools

python 2.6 引入了itertools模块,使得排列组合的实现非常简单: 复制代码 代码如下: import itertools 有序排列:e.g., 4个数内选2个排列: 复制代码 代码如下: >>> print list(itertools.permutations([1,2,3,4],2))[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4,

Python编程之黑板上排列组合,你舍得解开吗

考虑这样一个问题,给定一个矩阵(多维数组,numpy.ndarray()),如何shuffle这个矩阵(也就是对其行进行全排列),如何随机地选择其中的k行,这叫组合,实现一种某一维度空间的切片.例如五列中选三列(全部三列的排列数),便从原有的五维空间中降维到三维空间,因为是全部的排列数,故不会漏掉任何一种可能性. 涉及的函数主要有: np.random.permutation() itertools.combinations() itertools.permutations() # 1. 对0-

Python使用itertools模块实现排列组合功能示例

本文实例讲述了Python使用itertools模块实现排列组合功能.分享给大家供大家参考,具体如下: 一.笛卡尔积:itertools.product(*iterables[, repeat]) 直接对自身进行笛卡尔积: import itertools for i in itertools.product('ABCD', repeat = 2): print (''.join(i),end=' ') 输出结果: AA AB AC AD BA BB BC BD CA CB CC CD DA D

Python列表list排列组合操作示例

本文实例讲述了Python列表list排列组合操作.分享给大家供大家参考,具体如下: 排列 例如: 输入为 ['1','2','3']和3 输出为 ['111','112','113','121','122','123','131','132','133','211','212','213','221','222','223','231','232','233','311','312','313','321','322','323','331','332','333'] 实现代码: # -*-

Python实现的排列组合、破解密码算法示例

本文实例讲述了Python实现的排列组合.破解密码算法.分享给大家供大家参考,具体如下: 排列组合(破解密码) 1.排列 itertools.permutations(iterable,n) 参数一:要排列的序列, 参数二:要选取的个数 返回的是一个迭代对象,迭代器中的每一个元素都是一个元组 import itertools #概念:从n个不同元素中取出m(m≤n)个元素,按照一定的顺序排成一列,叫做从n个元素中取出m个元素的一个排列(Arrangement).特别地,当m=n时,这个排列被称作

Python使用combinations实现排列组合的方法

好久没有写博客了!昨天小牛在上海举办了牛友见面会,现场优惠还是比较大,心仪已久加上一时脑热就入手了.以为会有多么开心,其实目前最大的感受就是焦虑!担心电动车被偷,担心电池被偷,担心路上突然被交警叔叔拦下!我想,直到我的小牛真的被偷的那天,这种焦虑才会随之消失.唉,要说这样的焦虑怎么来的,我只能呵呵了:) 下面步入正题: 今天在帮我们家王博处理专利数据的时候,需要对专利数据中的城市和专利主体的合作关系数量进行统计.一项专利其写作主体可能由两个或两个以上主体参与,各个主体又分属不同或者相同的城市,对

Python实现的简单排列组合算法示例

本文实例讲述了Python实现的简单排列组合算法.分享给大家供大家参考,具体如下: 1.python语言简单.方便,其内部可以快速实现排列组合算法,下面做简单介绍 2.一个列表数据任意组合 主要是利用自带的库: #_*_ coding:utf-8 _*_ #__author__='dragon' import itertools list1 = [1,2,3,4,5] list2 = [] for i in range(1,len(list1)+1): iter = itertools.comb

Python标准库之itertools库的使用方法

前言 因为最近事情不是很多,想写一些技术文章分享给大家,同时也对自己一段时间来碎片化接受的知识进行一下梳理,所谓写清楚才能说清楚,说清楚才能想清楚,就是这个道理了. 很多人都致力于把Python代码写得更Pythonic,一来更符合规范且容易阅读,二来一般Pythonic的代码在执行上也更有效率.今天就先给大家介绍一下Python的系统库itertools.下面话不多说了,来一起看看详细的介绍吧. itertools库 迭代器(生成器)在Python中是一种很常用也很好用的数据结构,比起列表(l

Python2.7基于笛卡尔积算法实现N个数组的排列组合运算示例

本文实例讲述了Python2.7基于笛卡尔积算法实现N个数组的排列组合运算.分享给大家供大家参考,具体如下: 说明:本人前段时间遇到的求n个数组的所有排列组合的问题,发现笛卡尔积算法可以解决,但是网上搜索的只有Java版本的实现,于是自己试着用python实现,由于新手代码不太规范. 代码:本人封装了一个类Cartesian(笛卡尔),其中封装了变量和方法: 1.变量 datagroup : 表示n个list(python 中的list与其他编程中的数组定义类似)的集合,即一个二维数组 coun