【问题标题】:PyQt4 Calendar Widget - highlight the 1st and 15th of each monthPyQt4 日历小部件 - 突出显示每个月的 1 日和 15 日
【发布时间】:2018-06-29 00:14:55
【问题描述】:

很难让 PyQt4 日历小部件突出显示每个月的 1 日和 15 日。不幸的是,没有太多关于这方面的文档,所以任何帮助都会很棒。 Example: Red box around the 5th of the month every month

【问题讨论】:

    标签: python-2.7 pyqt4


    【解决方案1】:

    您必须从 QCalendarWidget 继承并覆盖 paintCell 方法:

    QCalendarWidget.paintCell(painter, rect, date)

    参数:

    画家——QtGui.QPainter

    rect – QtCore.QRect

    日期 – QtCore.QDate

    对不起,我用的是 PyQt5

    import sys
    from PyQt5.QtCore    import Qt, QRectF
    from PyQt5.QtGui     import QPainter
    from PyQt5.QtWidgets import QCalendarWidget, QApplication
    
    class CalendarWidget(QCalendarWidget):
        def paintCell(self, painter, rect, date):
            painter.setRenderHint(QPainter.Antialiasing, True)
            if (date.day() == 1) or (date.day() == 15):
                painter.save()
                painter.drawRect(rect)
                painter.setPen(Qt.blue)
                painter.drawText(QRectF(rect), Qt.TextSingleLine|Qt.AlignCenter, str(date.day()))
                painter.restore()
            else:
                QCalendarWidget.paintCell(self, painter, rect, date)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = CalendarWidget()
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 谢谢!完美运行!
    猜你喜欢
    • 2020-11-20
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多