2017-01-16 35 views
0

有問題我試圖讓我自己的科學計算器在wx,但例如,當涉及到計算3^4屏幕上顯示的是pow(x,y)問題是,我想這個計算出現像3^4 。這是我的代碼:與wxpython

from __future__ import division # So that 8/3 will be 2.6666 and not 2 
import wx 
from math import * 
from cmath import pi 

class Calculator(wx.Panel): 
    '''Main calculator dialog''' 
    def __init__(self, *args, **kwargs): 
     wx.Panel.__init__(self, *args, **kwargs) 
     sizer = wx.BoxSizer(wx.VERTICAL) # Main vertical sizer 

     self.display = wx.ComboBox(self) # Current calculation 
     sizer.Add(self.display, 0, wx.EXPAND|wx.BOTTOM, 8) # Add to main sizer 

     gsizer = wx.GridSizer(9,4, 8, 8) 
     for row in (("(",")","x^y","root"), 
        ("sin","cos","tan","e^x"), 
        ("arcsin","arccos","arctan","π"), 
        ("sinh","cosh","tanh","e"), 
        ("arcsinh","arccosh","arctanh","n!"), 
        ("7", "8", "9", "/"), 
        ("4", "5", "6", "*"), 
        ("1", "2", "3", "-"), 
        ("0", ".", "C", "+")): 
      for label in row: 
       b = wx.Button(self, label=label, size=(50,-1)) 
       gsizer.Add(b) 
       b.Bind(wx.EVT_BUTTON, self.OnButton) 
     sizer.Add(gsizer, 1, wx.EXPAND) 

     # [ =  ] 
     b = wx.Button(self, label="=") 
     b.Bind(wx.EVT_BUTTON, self.OnButton) 
     sizer.Add(b, 0, wx.EXPAND|wx.ALL, 8) 
     self.equal = b 

     # Set sizer and center 
     self.SetSizerAndFit(sizer) 

    def OnButton(self, evt): 
     '''Handle button click event''' 

     # Get title of clicked button 
     label = evt.GetEventObject().GetLabel() 

     if label == "=": # Calculate 
      self.Calculate() 

     elif label == "C": # Clear 
      self.display.SetValue("") 
     elif label == "x^y": 
      self.display.SetValue("pow(x,y)") 
     elif label == "x^2": 
      self.display.SetValue("pow(x,2)") 
     elif label == "10^x": 
      self.display.SetValue("pow(10,x)") 
     elif label == "e^x": 
      self.display.SetValue("exp") 
     elif label == "arcsin": 
      self.display.SetValue("asin(x)") 
     elif label == "arccos": 
      self.display.SetValue("acos") 
     elif label == "arcsinh": 
      self.display.SetValue("asinh") 
     elif label == "arccosh": 
      self.display.SetValue("acosh") 
     elif label == "arctanh": 
      self.display.SetValue("atanh") 
     elif label == "arctan": 
      self.display.SetValue("atan") 
     elif label == "n!": 
      self.display.SetValue("factorial") 
     elif label == "π": 
      self.display.SetValue("pi") 
     elif label == "root": 
      self.display.SetValue("sqrt") 



     #x^y,x^2,10^x 


     else: # Just add button text to current calculation 
      self.display.SetValue(self.display.GetValue() + label) 
      self.display.SetInsertionPointEnd() 
      self.equal.SetFocus() # Set the [=] button in focus 

    def Calculate(self): 
     """ 
     do the calculation itself 

     in a separate method, so it can be called outside of a button event handler 
     """ 
     try: 
      compute = self.display.GetValue() 
      # Ignore empty calculation 
      if not compute.strip(): 
       return 

      # Calculate result 
      result = eval(compute) 

      # Add to history 
      self.display.Insert(compute, 0) 

      # Show result 
      self.display.SetValue(str(result)) 
     except e: 
      wx.LogError(str(e)) 
      return 

    def ComputeExpression(self, expression): 
     """ 
     Compute the expression passed in. 

     This can be called from another class, module, etc. 
     """ 
     print ("ComputeExpression called with:"), expression 
     self.display.SetValue(expression) 
     self.Calculate() 

class MainFrame(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     kwargs.setdefault('title', "Calculator") 
     wx.Frame.__init__(self, *args, **kwargs) 

     self.calcPanel = Calculator(self) 

     # put the panel on -- in a sizer to give it some space 
     S = wx.BoxSizer(wx.VERTICAL) 
     S.Add(self.calcPanel, 1, wx.GROW|wx.ALL, 10) 
     self.SetSizerAndFit(S) 
     self.CenterOnScreen() 


if __name__ == "__main__": 
    # Run the application 
    app = wx.App(False) 
    frame = MainFrame(None) 
    frame.Show() 
    app.MainLoop() 
+0

此外,每次我點擊一個數學庫的項目,其他任何被刪除 –

+0

歡迎來到堆棧溢出!請查看我們的[SO問題清單](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)來幫助你提出一個好問題,從而得到一個很好的答案。 –

回答

0

你的問題是,你正在使用的math模塊的語法來計算結果。
您將與其他功能有同樣的問題。
你將不得不把功能分爲math語法
要解決您的pow(x,y)問題:

 elif label == "x^y": 
#   self.display.SetValue("pow(x,y)") 
      self.display.SetValue(self.display.GetValue() + "^") 

然後在您的Calculate功能:

if "^" in compute: 
     start, end = compute.split("^") 
     compute = "pow("+start+","+end+")" 

,你所擁有的其他相關問題,我離開給你解決。