TypeError: javaScriptConfirm() takes exactly 1 argument (3 given) python

TypeError: javaScriptConfirm() takes exactly 1 argument (3 given) python

self.browser = QWebView()
self.page = MyWebPage()
self.browser.setPage(self.page)

MyWebPage() class 是:

class MyWebPage(QWebPage):
    def javaScriptAlert(self):
        pass
    def javaScriptConfirm(self):
        pass
    def javaScriptConsoleMessage(self):
        pass
    def javaScriptPrompt(self):
        pass

然后我得到类型错误:

TypeError: javaScriptConfirm() takes exactly 1 argument (3 given)

谁能告诉我为什么会出现该错误?

如果要覆盖class函数的某些方法必须具有相同数量的参数,如果我们查看docs:

virtual void javaScriptAlert(QWebFrame * frame, const QString & msg)

virtual bool javaScriptConfirm(QWebFrame * frame, const QString & msg)

virtual void javaScriptConsoleMessage(const QString & message, int lineNumber, const QString & sourceID)

virtual bool javaScriptPrompt(QWebFrame * frame, const QString & msg, const QString & defaultValue, QString * result)

通过复习,您必须按如下方式覆盖方法:

class MyWebPage(QWebPage):
    def javaScriptAlert(self, frame, msg):
        pass
    def javaScriptConfirm(self, frame, msg):
        pass
    def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
        pass
    def javaScriptPrompt(self, frame, msg, defaultValue, result):
        pass

或者如果您不打算使用这些参数中的任何一个,您可以使用 *args**kwargs:

class MyWebPage(QWebPage):
    def javaScriptAlert(self, *args, **kwargs):
        pass
    def javaScriptConfirm(self, *args, **kwargs):
        pass
    def javaScriptConsoleMessage(self, *args, **kwargs):
        pass
    def javaScriptPrompt(self, *args, **kwargs):
        pass