Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:56:52+00:00 2026-06-14T23:56:52+00:00

I’d like to create a GUI app with some buttons and want to handle

  • 0

I’d like to create a GUI app with some buttons and want to handle button press events in same way (i.e. single event handle for all key presses). But I don’t understand how can button_hanlder understand which button was pressed.

Here is code

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

I want in button_handler determine which button was pressed, for example by button caption.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:56:53+00:00Added an answer on June 14, 2026 at 11:56 pm

    The QButtonGroup is designed for this. It will work with any kind of button which inherits QAbstractButton, such as QPushButton, QRadioButton, QCheckBox, etc.

    Just add all the buttons to the button-group using addButton and then connect the buttonClicked signal to a handler.

    NB: It may be a good idea to put all the buttons inside a container widget, such as a QGroupBox, QFrame or just a plain QWidget. This will allow you to loop over the container’s children and automatically add all the buttons to a button-group.

    Example Code:

    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))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to create an if statement in PHP that prevents a single post
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.