2014-01-18 21 views
0

我創建按鈕的字典,:PyQt的鈕釦字典

self.edit = QtGui.QLabel('') 
self.calcstring = '' 
self.button = {} 
for i in self.btn: 
    self.button[i] = QtGui.QPushButton(i) 
    if j == 2: 
     self.grid.addWidget(self.edit, 0, 2) 
    else: 
     self.grid.addWidget(self.button[i], pos[j][0], pos[j][1]) 
    j += 1 

現在都應該得到被點擊的功能:

for i in self.btn: 
    self.button[i].clicked.connect(self._action) 

的這個功能,按下的國家應該readed,我做到了這樣:

def _action(self): 
if self.button['1'].clicked(): 
    sys.exit() 
else: 
    self.update(self.calcstring) 

但在按一個鍵的錯誤:

if self.button['1'].clicked(): 
TypeError: native Qt signal is not callable 

有什麼不對?

回答

2

使用self.sender()獲得觸發事件的小部件,這裏有一個例子:

from PyQt4 import QtGui 

app = QtGui.QApplication([]) 
panel = QtGui.QWidget() 
vbox = QtGui.QVBoxLayout() 

def _action(): 
    print panel.sender().text() 

for text in ["help", "update", "exit"]: 
    button = QtGui.QPushButton(text) 
    button.clicked.connect(_action) 
    vbox.addWidget(button) 

panel.setLayout(vbox) 

panel.show() 
app.exec_() 

你的情況:

if self.sender() is self.button['1']: 
    ... 
+0

謝謝!作品! – Tekkzz