2017-01-16 1 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

Stack Overflow에 오신 것을 환영합니다! 좋은 질문을하고 좋은 대답을 얻으려면 [SO Question Checklist] (http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)를 검토하십시오. –

답변

0

귀하의 문제는 당신이 결과를 계산하기 위해 math 모듈의 구문을 사용하는 것입니다 : 이것은 내 코드입니다.
다른 기능과 동일한 문제가 발생합니다. 당신의 Calculate 기능에 다음

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

:

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

당신이 다른 관련 문제, 내가 떠날
당신은 당신의 pow(x,y) 문제를 해결하려면 math 구문
에 기능을 넣어해야합니다 해결할 당신에게.