Adding a button

The next customization is to add a button to our application. The most common button that is 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, such as a 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 emission. There can also be other signals that are provided on a button, such as 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, such as QToolButton, QRadioButton, QCommandLinkButton, and QCheckBox, which will be discussed later.

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

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

The parent parameter can be any widget, while 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(self.quitApp)

Add the preceding function to the earlier example class and call the function from your initGUI conditional block before calling the show() function of myWindow. The important point here is the clicked.connect() call of the myButton object. The clicked event connects to the myApp.quit() slot, 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 to provide a modal dialog box to inform the user, or to ask the user a question and receive an answer. We will see more in detail about the QMessageBox in Chapter 5, Dialogs and Widgets. Here, for modularity and reusability, we created function msgApp, which takes an argument title and message and returns a Yes or No. Just create an instance of it and add it to our program. This provides reusability of this function across the application. This function is called by quitApp to display the confirmation message using the given title.

To do this, create these functions as follows:

     #Function to Quit App that uses MsgApp
    def quitApp(self):
        response = self.msgApp("Confirmation","This will quit the application. Do you       
         want to Continue?")
        
        if response == "Y":
            myApp.quit()
        else:
            pass

    
# Function to show Dialog box with provided Title and Message
    def msgApp(self,title,msg):
        userInfo = QMessageBox.question(self,title,msg,
                                        QMessageBox.Yes | QMessageBox.No)
        if userInfo == QMessageBox.Yes:
            return "Y"
        if userInfo == QMessageBox.No:
            return "N"

Now, change the connect function in the setButton module to call the quitApp function on clicking 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
3.14.142.194