与wxpython有问题

与wxpython有问题,wxpython,Wxpython,我试图用wx制作我自己的科学计算器,但例如,当计算3^4时,屏幕上显示的是pow(x,y)问题是我希望这个计算看起来像3^4。这是我的代码: 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 dialo

我试图用wx制作我自己的科学计算器,但例如,当计算3^4时,屏幕上显示的是
pow(x,y)
问题是我希望这个计算看起来像3^4。这是我的代码:

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()

您的问题是您正在使用
math
模块的语法来计算结果。
您的其他功能也会遇到同样的问题。
您必须将函数放入
math
语法
要解决您的
pow(x,y)
问题:

        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+")"

您还有其他相关问题,我留给您解决。

此外,每次我单击数学库的一项时,其他任何内容都将被删除。请访问堆栈溢出!请复习我们的课程,帮助你提出一个好问题,从而得到一个好答案。