【问题标题】:PyQt Button-DictionaryPyQt 按钮字典
【发布时间】:2014-01-18 22:55:08
【问题描述】:

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

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

现在所有人都应该获得 Clicked-Function:

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

在这个函数中,应该读取 Pressed-State,我这样做了:

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

怎么了?

【问题讨论】:

    标签: python button dictionary pyqt


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2018-09-11
      相关资源
      最近更新 更多