Skip to content

Instantly share code, notes, and snippets.

@l-arkadiy-l
Created March 15, 2022 12:44
Show Gist options
  • Save l-arkadiy-l/41c474b5f65c58b97ce5543d484ee9e1 to your computer and use it in GitHub Desktop.
Save l-arkadiy-l/41c474b5f65c58b97ce5543d484ee9e1 to your computer and use it in GitHub Desktop.
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class WeekCalendar(QCalendarWidget):
def __init__(self, *args):
QCalendarWidget.__init__(self, *args)
self.color = QColor(255, 245, 8)
self.color.setAlpha(64)
self.selectionChanged.connect(self.updateCells)
def paintCell(self, painter, rect, date):
QCalendarWidget.paintCell(self, painter, rect, date)
first_day = self.firstDayOfWeek()
last_day = first_day + 6
current_date = self.selectedDate()
current_day = current_date.dayOfWeek()
if first_day <= current_day:
first_date = current_date.addDays(first_day - current_day)
else:
first_date = current_date.addDays(first_day - 7 - current_day)
last_date = first_date.addDays(6)
if first_date <= date <= last_date:
painter.fillRect(rect, self.color)
if __name__ == "__main__":
app = QApplication(sys.argv)
web = WeekCalendar()
web.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment