2012-05-24 227 views
6

我嘗試在PyQt類中添加一個函數,但它總是返回一個錯誤。在PyQt中創建動態按鈕

# Error: TypeError: connect() slot argument should be a callable or a signal, not 'NoneType' # 
def commander (self, arg): 
    exec arg  

def aButton (self, layout, **kwargs): 
    name = kwargs.pop("name","Button") 
    command = kwargs.pop("command", "") 
    button = QtGui.QPushButton(name) 
    button.clicked.connect(self.commander(command)) 
    layout.addWidget(button) 
    return button 

可能是有人在這裏能幫助我解決了:') THX!

回答

20

你需要一個函數:

button.clicked.connect(lambda: self.commander(command)) 

注拉姆達將避免函數調用的評價,所以點擊

+2

好'call' hehe .. –

+0

謝謝!有用 ! – MObject

+1

好吧!我是新來的;)thx再次:) – MObject

2

看來在

button.clicked.connect(self.commander(command)) 

self.commander(command)在返回None代替信號或調用。

+0

只有當是的,它會叫self.commander(command),但我只是想執行一些代碼當按鈕被點擊時。我不明白我需要回報什麼樣的價值。 return compile(arg,'','exec') 我嘗試過,但它是一樣的... – MObject