Centering the Window on the screen

Many windowed applications will get their importance by the way they are displayed on the screen. It is better practice for any GUI application to display the window centered on the screen. There are two advantages of doing this. One is to get the attention of the user and the other is to adjust with different display formats of 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 center to any screen. The method takes the object it is calling and centers it with respect to the screen it is displayed:

    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, first we 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 which 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. Remember to call this function before the myWindow.show() line to view the settings 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.145.35.247