【问题标题】:can't seem to fix PyCharm warnings in a simple PyQt program似乎无法在简单的 PyQt 程序中修复 PyCharm 警告
【发布时间】:2017-08-31 11:17:59
【问题描述】:

以下程序(我真实代码的摘录)

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()

在 PyCharm 中触发以下三个与 QMessageBox.warning 调用相关的警告(例如,在运行 Code/Inspect Code... 时)

  • 使用不同类的实例按类调用方法; 传递 PyQt4.QtGui.QPushButton.QPushButton 而不是 PyQt4.QtGui.QMessageBox.QMessageBox。这是故意的吗?

  • 调用参数不正确;参数'QString_1'未填充

  • 类型检查器;预期类型“QMessageBox”,改为“QPushButton”

还有一个与 PyQt connect 调用相关的警告

  • 在“功能”中找不到引用“连接”

知道如何解决/避免这些警告吗?

【问题讨论】:

标签: pycharm pyqt4


【解决方案1】:

我遇到了同样的问题。您正在向函数传递 QPushButton,在您的情况下是“w”。该函数需要一个 QMessageBox。所以只需像这样传递它:

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()

【讨论】:

猜你喜欢
  • 2017-01-09
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
相关资源
最近更新 更多