Adding a button

The next customization is to add a button to our application. The most common button used in any GUI is a push or command button. We push (click on) the button to command the computer to perform some action or answer a decision. Typical push buttons include OK, Apply, Cancel, Close, Yes, No, and Help. Usually the push button is rectangular with some label on it. It can also have an optional icon associated with it. It is also possible to define a shortcut key combination to the button to which it responds on clicking the key combination.

The button emits a signal when it is activated by any external event, say, mouse click or by pressing the spacebar or by a keyboard shortcut. A widget may be associated with this key click event which is executed on receiving this signal and it is usually called a slot, in Qt. We will learn more about signals and slots in the later chapters. As for now, be informed that a signal will connect to a slot on emit. There can also be other signals that are provided on a button like button pressed, button released, button checked, button down, button enabled, and so on. Apart from a push button we also have other button types in Qt like QToolButton, QRadioButton, QCommandLinkButton and QCheckBox will be discussed later.

QPushButton can be instantiated in three ways. It has three constructors with different signatures. They are:

QPushButton(parent=None)
QPushButton(text, [parent=None])
QPushButton(icon, text, [parent=None])

The parent parameter can be any widget, text is any string or a set of unicode characters, and icon is a valid QIcon object.

In this example program, we are going to add a button that will close the application when clicked. We define a button first and will call a function (slot) when clicked (signal).

    def setButton(self):
        """ Function to add a quit button
        """
        myButton = QPushButton('Quit', self)
        myButton.move(50, 100)
        myButton.clicked.connect(myApp.quit)

Add the preceding function to the earlier example class and call the function from your __main__ conditional block before calling the show() function of myWindow. The important point here is the clicked.connect() call of the myButton object. The event clicked connects to the slot myApp.quit() which quits the application. The slot can be replaced by an excerpt of code or a user defined function which performs a set of operations.

It is highly likely that the quit button may be pressed by mistake. If the application is quit without the user's confirmation there is a high chance of it being a mistake. So, we are going to display a confirmation message to the user on clicking the quit button. If the user wishes to quit, the application quits or the user can cancel it. The widely used widget for this purpose is QMessageBox. This is used for providing a modal dialog box for informing the user or for asking the user a question and receiving an answer. We will see more in detail about the QMessageBox in Chapter 5, Dialogs and Widgets. Here, we just create an instance of it and add it to our program.

To do this, create a function as follows:

    def quitApp(self):
        """ Function to confirm a message from the user
        """
        userInfo = QMessageBox.question(self, 'Confirmation',"This will quit the application. Do you want to Continue?",
         QMessageBox.Yes | QMessageBox.No)
        if userInfo == QMessageBox.Yes:
            myApp.quit()
        if userInfo == QMessageBox.No:
            pass

Now, change the connect function in the setButton module to call this function on click of the quit button:

myButton.clicked.connect(self.quitApp)

On executing the program and clicking on the quit button, you will get a confirmation message for which you can click on Yes or No:

Adding a button

Clicking on Yes will quit the app and clicking on No will do nothing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset