4

我想创建一个带有一些按钮的 GUI 应用程序,并希望以相同的方式处理按钮按下事件(即所有按键的单个事件句柄)。但我不明白 button_hanlder 如何理解按下了哪个按钮。

这是代码

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(350, 190, 98, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(240, 220, 98, 27))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(350, 250, 98, 27))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(460, 220, 98, 27))
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName(_fromUtf8("menuFile"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuFile.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("pressed()")), self.button_handler)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("pressed()")), self.button_handler)
        QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL(_fromUtf8("pressed()")), self.button_handler)
        QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("pressed()")), self.button_handler)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Up", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow", "Right", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_3.setText(QtGui.QApplication.translate("MainWindow", "Down", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_4.setText(QtGui.QApplication.translate("MainWindow", "Left", None, QtGui.QApplication.UnicodeUTF8))
        self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))

    def button_handler(self):
        # Which button was pressed?

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我想在 button_handler 中确定按下了哪个按钮,例如通过按钮标题。

4

2 回答 2

3

QButtonGroup就是为此而设计的。它适用于任何类型的继承按钮QAbstractButton,例如QPushButton, QRadioButton,QCheckBox等。

只需使用addButton将所有按钮添加到按钮组,然后将buttonClicked信号连接到处理程序。

注意:将所有按钮放在容器小部件中可能是个好主意,例如 a QGroupBoxQFrame或者只是一个普通的QWidget。这将允许您遍历容器的子项并自动将所有按钮添加到按钮组。

示例代码:

btngroup.py

from PyQt4 import QtGui, QtCore
from btngroup_ui import Ui_ButtonGroup

class Window(QtGui.QWidget, Ui_ButtonGroup):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)
        self.buttonGroup = QtGui.QButtonGroup(self)
        for button in self.buttonBox.findChildren(QtGui.QAbstractButton):
            self.buttonGroup.addButton(button)
        self.buttonGroup.buttonClicked.connect(self.handleButtonClicked)

    def handleButtonClicked(self, button):
        print('"%s" was clicked' % button.text())

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

btngroup_ui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'btngroup.ui'
#
# Created: Wed Nov 21 17:50:42 2012
#      by: PyQt4 UI code generator 4.9.5
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_ButtonGroup(object):
    def setupUi(self, ButtonGroup):
        ButtonGroup.setObjectName(_fromUtf8("ButtonGroup"))
        ButtonGroup.resize(240, 167)
        self.horizontalLayout = QtGui.QHBoxLayout(ButtonGroup)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.buttonBox = QtGui.QWidget(ButtonGroup)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.verticalLayout = QtGui.QVBoxLayout(self.buttonBox)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.buttonA = QtGui.QPushButton(self.buttonBox)
        self.buttonA.setObjectName(_fromUtf8("buttonA"))
        self.verticalLayout.addWidget(self.buttonA)
        self.buttonB = QtGui.QPushButton(self.buttonBox)
        self.buttonB.setObjectName(_fromUtf8("buttonB"))
        self.verticalLayout.addWidget(self.buttonB)
        self.buttonC = QtGui.QPushButton(self.buttonBox)
        self.buttonC.setObjectName(_fromUtf8("buttonC"))
        self.verticalLayout.addWidget(self.buttonC)
        self.buttonD = QtGui.QPushButton(self.buttonBox)
        self.buttonD.setObjectName(_fromUtf8("buttonD"))
        self.verticalLayout.addWidget(self.buttonD)
        self.horizontalLayout.addWidget(self.buttonBox)

        self.retranslateUi(ButtonGroup)
        QtCore.QMetaObject.connectSlotsByName(ButtonGroup)

    def retranslateUi(self, ButtonGroup):
        ButtonGroup.setWindowTitle(QtGui.QApplication.translate("ButtonGroup", "Button Group", None, QtGui.QApplication.UnicodeUTF8))
        self.buttonA.setText(QtGui.QApplication.translate("ButtonGroup", "Button A", None, QtGui.QApplication.UnicodeUTF8))
        self.buttonB.setText(QtGui.QApplication.translate("ButtonGroup", "Button B", None, QtGui.QApplication.UnicodeUTF8))
        self.buttonC.setText(QtGui.QApplication.translate("ButtonGroup", "Button C", None, QtGui.QApplication.UnicodeUTF8))
        self.buttonD.setText(QtGui.QApplication.translate("ButtonGroup", "Button D", None, QtGui.QApplication.UnicodeUTF8))
于 2012-11-21T18:26:47.600 回答
0

您可能想要使用QtCore.QSignalMapper.

有关详细说明,请参阅http://srinikom.github.com/pyside-docs/PySide/QtCore/QSignalMapper.html

基本上,它允许您将字符串值关联到按钮和插槽之间的每个连接,因此您可以通过分析信号的字符串参数的内容来检查单击来自哪个按钮。

于 2012-11-21T17:20:05.040 回答