PyQt按钮功能

2024-04-27 03:18:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个按钮字典:

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

现在所有人都应该得到点击函数:

^{pr2}$

在这个函数中,按下的状态应该被读取,我这样做了:

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

怎么了?在


Tags: 函数posselfif字典buttonedit按钮
1条回答
网友
1楼 · 发布于 2024-04-27 03:18:26

使用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_()

在您的情况下:

^{pr2}$

相关问题 更多 >