TypeError: accept missing 2 required arguments (St

2019-09-04 14:23发布

问题:

This is a curiosity question. I have a block of code that executes upon an accepted.connect(). It throws out this error but when I check the results they still work. And from what I understand from button box: upon pressing OK or cancel, the dialog should close on it's own. I haven't even initialized self.buttonBox.canceled.connect but when I push cancel the window closes. When I push OK though the window stays open and gives the following error:

TypeError: accept() missing 2 required positional arguments: 'player' and 'window'

So here's some code:

class UIinit(QtGui.QDialog, UI.Buy_Cargo.Ui_Dialog):
        """The Dialog for initiation"""
        def __init__(self, player, window):
            super(UIinit, self).__init__()
            self.window = window
            self.player = player
            self.setupUi(self)
            #for i in Config.port_prices:
            #   up = getattr(self, "btn_" + i + "_up")
            #    up.clicked.connect(partial(self.up, i, player))
            #    down = getattr(self, "btn_" + i + "_down")
            #    down.clicked.connect(partial(self.down, i))
            #    add_price = getattr(self, "H_" + i)
            #    add_price.setText(i +
            #        " (" + str(self.player.port.price[i]) + ")")
            self.buttonBox.accepted.connect(  # the important part
                partial(self.accept, player, window))
        def accept(self, player, window):
            if int(self.V_Total.text()) <= player.money:
                for i in player.ship.cargo:
                    num = int(getattr(self, "V_" + i).text())
                    player.ship.cargo[i] += num
                window.to_shipyard()
dia = UIinit(player, window)
dia.exec_()

I've tried connect(lambda: self.accept(player, window)) and using self.player and self.window as the arguments.

回答1:

This simplified snippets below does work as expected, so your problem is elsewhere. Just a wildguess - if you copy/pasted you code, there are tabs in it and if you have a mix of tabs and spaces you can get really weird results - like having def accept actually being defined in __init__ itself instead of being a proper method, in which case self is not automagically passed to the function. Doesn't really match your error message but that might be worth checking....

from functools import partial

class Button(object):
    def __init__(self):
        self.callbacks = []
    def connect(self, callback):
        self.callbacks.append(callback)
    def click(self):
        for callback in self.callbacks:
            callback()

class UI(object):
    """The Dialog for initiation"""
    def __init__(self, player, window):
        self.window = window
        self.player = player
        self.button = Button()
        self.button.connect(  # the important part
            partial(self.accept, player, window))
        self.button.connect( 
            lambda: self.accept(player, window))

    def accept(self, player, window):
        print "accept %s %s" % (player, window)

    def exec_(self):
        self.button.click()
player = "p"
window = "w"
dia = UI(player, window)
dia.exec_()


回答2:

It seems that it has to do with me overwriting the dialog's accept function. Once I changed the accept() to GoNinjaGoNinjaGo() everything worked just fine.