Calendar/Date picker

#!/usr/bin/env python
# [SNIPPET_NAME: Calendar/Date picker]
# [SNIPPET_DESCRIPTION: A calendar/date picker example]
# [SNIPPET_AUTHOR: Darren Worrall <[email protected]>]

# example

import sys
from datetime import timedelta
from PyQt4 import QtGui, QtCore

class Calendar(QtGui.QWidget):
    A QCalendarWidget example

    def __init__(self):
        # create GUI
        self.setWindowTitle('Calendar widget')
        # Set the window dimensions
        # vertical layout for widgets
        self.vbox = QtGui.QVBoxLayout()
        # Create a calendar widget and add it to our layout = QtGui.QCalendarWidget()

        # Create a label which we will use to show the date a week from now
        self.lbl = QtGui.QLabel()
        # Connect the clicked signal to the centre handler
        self.connect(, QtCore.SIGNAL('selectionChanged()'), self.date_changed)

    def date_changed(self):
        Handler called when the date selection has changed
        # Fetch the currently selected date, this is a QDate object
        date =
        # This is a gives us the date contained in the QDate as a native
        # python date[time] object
        pydate = date.toPyDate()
        # Calculate the date a week from now
        sevendays = timedelta(days=7)
        aweeklater = pydate + sevendays
        # Show this date in our label
        self.lbl.setText('The date in a week will be: %s' % aweeklater)

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