Center window

#!/usr/bin/env python
# [SNIPPET_NAME: Center window]
# [SNIPPET_DESCRIPTION: A example of how to centre a window]
# [SNIPPET_AUTHOR: Darren Worrall <>]

# example

import sys
from PyQt4 import QtGui, QtCore

class CentreWindow(QtGui.QWidget):
    An example of how to centre a window

    def __init__(self):
        # create GUI
        self.setWindowTitle('Centre window')
        # Set the window dimensions
        # vertical layout for widgets
        self.vbox = QtGui.QVBoxLayout()
        # Create a push button labelled 'Centre' and add it to our layout
        btn = QtGui.QPushButton('Centre', self)
        # Connect the clicked signal to the centre handler
        self.connect(btn, QtCore.SIGNAL('clicked()'), self.centre)

    def centre(self):
        Center the window on screen. This implemention will handle the window
        being resized or the screen resolution changing.
        # Get the current screens' dimensions...
        screen = QtGui.QDesktopWidget().screenGeometry()
        # ... and get this windows' dimensions
        mysize = self.geometry()
        # The horizontal position is calulated as screenwidth - windowwidth /2
        hpos = ( screen.width() - mysize.width() ) / 2
        # And vertical position the same, but with the height dimensions
        vpos = ( screen.height() - mysize.height() ) / 2
        # And the move call repositions the window
        self.move(hpos, vpos)

# If the program is run directly or passed as an argument to the python
# interpreter then create a CentreWindow instance and show it
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    gui = CentreWindow()