如何从事件对象(Qt + Python + PySide2)获取对按钮的引用?

时间:2019-06-24 15:35:09

标签: python pyside2

我正在PySide2的帮助下以Python脚本构建Qt UI。我希望有几个按钮(可以是几十个),它们在按下按钮时分配给它们相同的功能,并且我希望能够在函数内部告诉哪个事件发送了事件。这是我到目前为止的代码:

def addButton(title, layout):
   widget_button = QtWidgets.QPushButton(title)
   layout.addWidget(widget_button)
   return widget_button
...
button_test = addButton("TEST", block_2.layout)  
button_test2 = addButton("TEST2", block_2.layout) 
...
def test(event):
    # something to check the button object, like 
    # if event.self == button_test: 
...
button_test.mousePressEvent = test
button_test2.mousePressEvent = test

0 个答案:

没有答案