【问题标题】:Cannot connect buttons to an event in PyQT无法将按钮连接到 PyQT 中的事件
【发布时间】:2017-07-01 11:11:56
【问题描述】:

我一直在尝试将按钮连接到函数,但出现以下错误:

QObject.connect(b1,SIGNAL("clicked()"),GetBestMatch)           
TypeError: arguments did not match any overloaded call:
  QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): first argument of unbound method must have type 'QObject'

这是我写下的给出错误的连接代码:

QObject.connect(b1,SIGNAL("clicked()"),GetBestMatch) 

我也尝试通过以下代码进行连接:

b1.clicked.connect(GetBestMatch)

并得到错误:

b1.clicked.connect(GetBestMatch)         
AttributeError: 'NoneType' object has no attribute 'clicked'

我不知道我做错了什么。

我能够创建一个带有标签和按钮的网格,但不能将按钮连接到功能。这是图形用户界面代码:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def MainWindow():
    app = QApplication(sys.argv)
    screen_resolution = app.desktop().screenGeometry()
    width, height = screen_resolution.width(), screen_resolution.height()
    win = QWidget()
    win.adjustSize()
    grid=QGridLayout()
    grid.setRowStretch(0, 1)
    grid.setRowStretch(1, 1)
    grid.setRowStretch(5, 1)
    for i in range(0,5):
        for j in range(0,4):
            if i==0 and j==2:
               l1=grid.addWidget(QLabel("Choose an option:"),i,j, 2, 2)
            if i==2 and j==1:
                b1=grid.addWidget(QPushButton("Get Best Match"),i,j)
            elif i==2 and j==2:
                b2=grid.addWidget(QPushButton("Button 2"),i,j)
            elif i==2 and j==3:
                b3=grid.addWidget(QPushButton("Button 3"),i,j)
    b5=grid.addWidget(QLabel(""),3,4) 
    b4=grid.addWidget(QPushButton("Button 4"),2,4)         
    win.setLayout(grid)
    win.setGeometry(100,100,width//2,height//2,)
    win.setWindowTitle("Information on all tracking buses")
    win.show()
    win.setStyleSheet("""
    .QPushButton {
    height: 30px ;
    width: 20px ; 
    }
    .QLabel {
    qproperty-alignment: AlignCenter;
    font-size:12pt
    }

    """)
    sys.exit(app.exec_())

def GetBestMatch():
    print ("HI")
if __name__ == '__main__':
    MainWindow()

布局完美无误。你能帮我解决这个问题吗?

【问题讨论】:

    标签: python-3.x user-interface pyqt4


    【解决方案1】:

    QLayout.addWidget 不返回任何内容: http://doc.qt.io/qt-5/qlayout.html#addWidget

    所以当你这样做时

    b1=grid.addWidget(QPushButton("Get Best Match"),i,j)
    

    一切都很好,但是因为b1None - 正如错误消息清楚地表明的那样。 因此,您需要为此花费两行代码:

    b1 = QPushButton("Get Best Match")
    grid.addWidget(b1, i, j)
    

    那你应该很好。

    【讨论】:

    • 太棒了!谢谢塞巴斯蒂安!希望你有一个美好的一天:)
    • 不客气 :) 请接受我的回答,如果有帮助 ;)
    • 你的意思是点赞?可悲的是,因为我是新用户,我没有“被允许”投票:(
    • 我有一个后续问题:如果我在 GetBestMatch 函数中创建一个新窗口,该窗口会打开但会立即关闭。你知道为什么吗?这是函数的代码: def GetBestMatch(): win1 = QWidget() win1.adjustSize() win1.setGeometry(100,100,width//2,height//2,) win1.setWindowTitle("Get Best Match") win1.show()
    猜你喜欢
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多