Python PyQt按钮字典

Python PyQt按钮字典,python,button,dictionary,pyqt,Python,Button,Dictionary,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.b

我正在创建一个按钮字典,其中包含:

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)
在这个函数中,按下的状态应该被读取,我这样做了:

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
怎么了?

使用
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']:
    ...