似乎无法在简单的 PyQt 程序中修复 PyCharm 警告 [英] can't seem to fix PyCharm warnings in a simple PyQt program

查看:81
本文介绍了似乎无法在简单的 PyQt 程序中修复 PyCharm 警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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?

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

    Incorrect call arguments; Parameter 'QString_1' unfilled

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

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

  • 和一个与 PyQt connect 调用相关的警告

    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?

    推荐答案

    我遇到了同样的问题.您将函数传递给 QPushButton,在您的情况下为w".该函数需要一个 QMessageBox.所以就这样传递它:

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

    这篇关于似乎无法在简单的 PyQt 程序中修复 PyCharm 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆