Home » Python

Event Handling Examples - PyQt

Event Handling Examples - PyQt: This article is a continuous content of the previous article. We went through the theory part of Event handling mechanism in PyQt along with an example of “Mouse click-event”. Here we are going to study about few other important events, signals, and slots through elaborative examples and practical codes. Right here, we are going to study the push button event. Just go through the steps of the code, do some alterations on your own; Stuck in trouble? Feel free to ask in the comment section.
Submitted by Atul Anand, on December 14, 2017

The Button takes your command

1. Getting output on Python shell or a CMD prompt

Before, we go digging deep into the source code and its fascinating output; let me help you with few key sections of this program:

  • window() is a user-defined function here, in which we have designed and plotted our layout framework.
  • We have used a Dialog box here using QDialog(). You can differentiate it from the main window (containing minimize and maximize buttons in there) by observing a "?" mark beside the close window red button at the top right corner.
  • The functionality of both the buttons have been designed using two user-built functions namely, b1_pushed() and b2_pushed().
  • window() function is getting called causing the interface to be executed when the program enters the main loop.

Source Code:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

def window():
   app = QApplication(sys.argv)
   layout = QDialog()
   b1 = QPushButton(layout)
   b1.setText("Push 1")
   b2 = QPushButton(layout)
   b2.setText("Push 2")


def b1_pushed():
   print ("Button 1 pressed")

def b2_pushed():
   print ("Button 2 pressed")

if __name__ == '__main__':


event handling in PyQt 1

The output generated by the event is shown on the Python Shell. There’s another way of doing it. Let’s check it out here:

event handling in PyQt 1

Well, here we are getting the output on the CMD prompt window. We can get this output by simply running the python file by double clicking on it. Let’s make the output preferences more interesting by printing it on the Dialog box/ layout window itself. We have done some modifications in program to get that.

2. Getting output on the Main Window itself

Let’s again figure out the key sections of the 2nd program written over here before going through the execution of the source code:

  • Example() is a user built function containing all the implementation codes. This is getting called when the program enters the main loop.
  • QMainWindow() is being used as an argument to the Example() function and is being used to make the main window layout. It can be differentiated easily from dialog box. We can also resize it by dragging from corners.
  • self.statusBar().showMessage() method is used for message passing with string as an argument message.
  • Message is being printed n the main window area in the statusbar section, also a widget.

Source Code:

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QMainWindow): 
    def __init__(self):
        super(Example, self).__init__()
    def initInterface(self):      

        b1 = QtGui.QPushButton("     Push-button 1", self)
        b1.move(30, 50)
        b2 = QtGui.QPushButton("     Push-button 2", self)
        b2.move(150, 50)
        self.setGeometry(400, 400, 290, 150)
        self.setWindowTitle(' Push button "sender" ')
    def buttonpushed(self):
        sender = self.sender()
        self.statusBar().showMessage('            ' + sender.text() + ' was pressed')
def main():  
    app = QtGui.QApplication(sys.argv)
    ex = Example()

if __name__ == '__main__':


event handling in PyQt 1

Here, you can see that the output is generated on the main window itself by message passing mechanism using a widget named statusbar.


Finally, at the end of this article, I would like to conclude that getting signals and handling slots is much easier than it looks by visuals. I have pointed out some key notes from selected sections of the code. The rest of the task is left on you to do some ambiguous trials and come out with interesting results. I would recommend my readers to make it more than just a passive reading of the article. Get yourself actively involved in it. Don’t just ask queries, post your findings too if you come up with something interesting and would like to share with your peer readers. Next article will cover few more responsive event handling examples. Catch you later in the next article. HAPPY LEARNING!

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.