简体   繁体   中英

wxpython error - “compute() takes exactly 2 arguments (1 given)” but it has 2 arguments

So recently i'v been trying to learn wxpython and I keep getting this error, I know the error says there isn't enough arguments but under the compute function it has both arguments.

import wx
import math
class game(wx.Frame):

    def __init__(self,parrent,id):

        c3 = "0"

        wx.Frame.__init__(self,parrent,id,"Form", size=(250,160))
        panel=wx.Panel(self)
        box2=wx.TextEntryDialog(None, "Input b", "Pythagorean theorem", "")
        if box2.ShowModal()==wx.ID_OK:
            b=box2.GetValue()
        box1=wx.TextEntryDialog(None, "Input A", "Pythagorean theorem", "")
        if box1.ShowModal()==wx.ID_OK:
            a=box1.GetValue()

        def compute(self, event):
            a2=int(a)**2
            b2=int(b)**2
            c = a2 + b2
            c2=math.sqrt(c)
            c3=str(c2)

        button=wx.Button(panel, label="Compute",pos=(90,70), size=(60,40))
        self.Bind(wx.EVT_BUTTON, compute, button)


        wx.StaticText(panel, -1, c3, (10,10))



if __name__=="__main__":
    app=wx.PySimpleApp()
    frame=game(parrent=None, id=-1)
    frame.Show()
    app.MainLoop()

error : "compute() takes exactly 2 arguments (1 given)"

nested functions dont get or require the self argument

class XYZ:
    def __init__(self):
        def some_func(x):
            print "SELF:",self
            print "X:",x
        some_func(6)


>>> print XYZ()
SELF: <__main__.XYZ instance at 0x029F7148>
X: 6
<__main__.XYZ instance at 0x029F7148>

this is because nested functions have access to all of their parent functions local variables (ie self)

in your instance just remove self from the argument list or make compute a class method instead of a method inside a class method

您将自己绑定为self.Bind(wx.EVT_BUTTON, compute, button)但是compute接受了(self, evt)参数,因此您需要将__init__计算移到游戏类中,并将其绑定为self.compute

The event handler, 'compute', is not a method here, but a function inside a method ( _ init _ ). If you add 'self' as an argument it expects an instance of parent class to be passed whenever it is called, which you didn't do specifically (not that you should). When you bind an event wx automatically sends in the event to the handler and if your handler accepts it, well...you can use it for further actions. A handler can be any function like a method or a lambda function etc. So, that being said, here you can just remove the 'self' argument or move the handler in to its own method. By the way, if that is the only button in the code, you don't need to specifically bind the event to that button. Also, will this work? you're expecting a label to display the results, right? Since c3 is inside that function I'm thinking it won't! (you might need your method to call SetLabel())

You need to move the definition on "compute" out of init . Also, declare panel belonging to the class and make a handler for the static text, since after you press the button the static text must be updated with the new result. You can also save some space and not allocate "c" in init :

class game(wx.Frame):

def __init__(self,parrent,id):

    wx.Frame.__init__(self,parrent,id,"Form", size=(250,160))
    self.panel=wx.Panel(self)

    box2=wx.TextEntryDialog(None, "Input B", "Pythagorean theorem", "")
    if box2.ShowModal()==wx.ID_OK:
        self.b=box2.GetValue()

    box1=wx.TextEntryDialog(None, "Input A", "Pythagorean theorem", "")
    if box1.ShowModal()==wx.ID_OK:
        self.a=box1.GetValue()

    button=wx.Button(self.panel, label="Compute", pos=(90,70), size=(60,40))
    self.Bind(wx.EVT_BUTTON, self.compute, button)
    self.st = wx.StaticText(self.panel, -1, "", (10,10))

def compute(self, event):
    c = str(math.sqrt(int(self.a)**2 + int(self.b)**2))
    self.st = wx.StaticText(self.panel, -1, c, (10,10))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM