Created
October 16, 2013 04:01
-
-
Save WhyNotHugo/7002512 to your computer and use it in GitHub Desktop.
A very simple, yet stylish calendar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Copyright (c) 2013 Hugo Osvaldo Barrera <hugo@osvaldobarrera.com.ar> | |
# | |
# Permission to use, copy, modify, and distribute this software for any | |
# purpose with or without fee is hereby granted, provided that the above | |
# copyright notice and this permission notice appear in all copies. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
from PySide.QtGui import QCalendarWidget, QMainWindow, QApplication, \ | |
QVBoxLayout, QWidget, QGridLayout, QLabel | |
from PySide.QtCore import Qt | |
from datetime import date, timedelta | |
import sys | |
# TODO: update today when the day changes | |
class MonthWidget(QWidget): | |
def __init__(self, today, month): | |
super(MonthWidget, self).__init__() | |
layout = QGridLayout() | |
self.setLayout(layout) | |
first = date(date.today().year, month, 1).weekday() | |
# I start on sundays and count staring on one (not zero) | |
first = ((first + 1) % 7 ) + 1 | |
if month < 12: | |
last = (date(date.today().year, month + 1, 1) - timedelta(1)).day | |
else: | |
last = (date(date.today().year + 1, 1, 1) - timedelta(1)).day | |
day = 0 | |
for i in range(0, 5): | |
for j in range(1, 8): | |
if i*7 + j < first: | |
continue | |
day += 1 | |
if day is today: | |
color = "red" | |
elif (j is 1 or j is 7): | |
color = "gray" | |
else: | |
color = "black" | |
l = QLabel('<font color="{}">{}</font>'.format(color, str(day))) | |
l.setAlignment(Qt.AlignHCenter) | |
layout.addWidget(l, i, j) | |
if day is last: | |
break | |
if day is last: | |
break | |
class MainWindow(QMainWindow): | |
def __init__(self, parent=None): | |
super(MainWindow, self).__init__(parent) | |
self.centralWidget = QWidget() | |
self.setCentralWidget(self.centralWidget) | |
# # Main Layout | |
# layout = QVBoxLayout() | |
# layout.setSpacing(0) | |
# self.centralWidget.setLayout(layout) | |
# cal = QCalendarWidget() | |
# cal.setSelectionMode(QCalendarWidget.NoSelection) | |
# cal.setHorizontalHeaderFormat(QCalendarWidget.NoHorizontalHeader) | |
# layout.addWidget(cal) | |
self.setStyleSheet("QWidget { background-color: white; }") | |
layout = QGridLayout() | |
self.centralWidget.setLayout(layout) | |
this_month = date.today().month | |
today = date.today().day | |
month = 0 | |
for r in range(0,3): | |
for c in range (0, 4): | |
month +=1 | |
m = None | |
if month is this_month: | |
m = MonthWidget(today, month) | |
else: | |
m = MonthWidget(None, month) | |
layout.addWidget(m, r, c) | |
app = QApplication(sys.argv) | |
w = MainWindow() | |
w.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment