can't seem to fix PyCharm warnings in a simple PyQt program

六月ゝ 毕业季﹏ 提交于 2021-02-08 15:06:39

问题


The following program (an extract of my real code)

from PyQt4 import QtGui
import sys
from PyQt4.QtGui import QMessageBox


def main():
    app = QtGui.QApplication([])

    w = QtGui.QPushButton("Test")

    def on_pressed():
        print("Pressed")
        QMessageBox.warning(w, 'Info', 'Button Pressed')

    w.pressed.connect(on_pressed)
    w.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

triggers the following three warnings in PyCharm (eg when running Code/Inspect Code... ) related to the QMessageBox.warning call

  • Calling a method by class using an instance of a different class; Passing PyQt4.QtGui.QPushButton.QPushButton instead of PyQt4.QtGui.QMessageBox.QMessageBox. Is this intentional?

  • Incorrect call arguments; Parameter 'QString_1' unfilled

  • Type checker; Expected type 'QMessageBox', got 'QPushButton' instead

and one warning related to the PyQt connect call

  • Cannot find reference 'connect' in 'function'

Any idea how I can work-around/avoid these warnings?


回答1:


I came across the same problem. You're passing the function a QPushButton, in your case the 'w'. The function is expecting a QMessageBox. So just pass it one like this:

from PyQt4 import QtGui
import sys
from PyQt4.QtGui import QMessageBox


def main():
    app = QtGui.QApplication([])

    w = QtGui.QPushButton("Test")
    dialog = QtGui.QMessageBox()

    def on_pressed():
        print("Pressed")
        QMessageBox.warning(dialog, 'Info', 'Button Pressed', 'Okay')

    w.pressed.connect(on_pressed)
    w.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()


来源:https://stackoverflow.com/questions/43238014/cant-seem-to-fix-pycharm-warnings-in-a-simple-pyqt-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!