2017-02-13 20 views
1

我一直在努力的按鈕連接到一個功能,我得到以下錯誤:不能按鈕連接到一個事件在PyQt的

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' 

我不知道我在做什麼錯。

我能夠創建一個帶標籤和按鈕的網格,但不能將按鈕連接到函數。這是GUI代碼:

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

佈局工作完美無誤。你能幫我解決這個問題嗎?

回答

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) 

那麼你應該不錯。

+0

它woooorks!感謝塞巴斯蒂安!希望你有一個偉大的一天:) – Sarah

+0

不客氣:)請接受我的答案,身份證它幫助;) – sebastian

+0

你的意思是upvote它?不幸的是,因爲我是新用戶,所以我不被允許上傳:( – Sarah

相關問題