Centering the window on the screen

The windowed applications based on type and functionality get displayed in the center for main applications, top right for notifications, and as an icon in the status bar of the screen. It is best practice for any main GUI application to display the window centered on the screen. There are two advantages of doing this. The first is to get the attention of the user, and the other is to adjust to different display formats of the various monitor screens.

There is no straightforward method to center the window by calling a predefined function of some class. Therefore, we write our own method, called center, to position the window central to any screen. This method takes the object that it is calling and centers it with respect to the screen that it is displayed on:

    def center(self):
        """ Function to center the application
        """
        qRect = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        qRect.moveCenter(centerPoint)
        self.move(qRect.topLeft())

In order to do this, we first get the size and location of the window that we want to be centered. Then, we need to get the center point of the screen. Finally, we will move the window to the center of the screen. The frameGeometry() function will return a PySide.QtCore.QRect object that will hold the height, width, top, and left points of the window. The QDesktopWidget().availableGeometry().center() call will return the center point of the screen. The next two lines will move the window to the center point of the screen. Please remember to call this function before the myWindow.show() line to view the settings that are applied on your window.

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

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