问题
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