PyQt5 QCalendarWidget 为选定的日期设置文本

PyQt5 QCalendarWidget 为选定的日期设置文本

在这篇文章中,我们将看到如何为QCalendarWidget的选定日期设置文本。为了给选定的日期添加文本,我们必须使用paintCell方法,它是虚拟的保护方法,可以通过覆盖方法来使用,因为这个方法是在paintEvent()中调用的。下面是文本的外观

PyQt5 QCalendarWidget - 为选定的日期设置文本

实施步骤:
1.创建继承了QCalendarWidget的日历类
2.在日历类中覆盖 paintCell 方法
3.创建一个主窗口
4.在主窗口中添加日历对象

下面是日历类的代码

# QCalendarWidget Class
class Calendar(QCalendarWidget):

    def paintCell(self, painter, rect, date):
        super(Calendar, self).paintCell(painter, rect, date)

        # checking if date is selected date
        if date == self.selectedDate():

            # saving the painter
            painter.save()

            # creating a QFont object
            font = QFont()

            # setting pixel size of the font
            font.setPixelSize(11)

            # making font bold
            font.setBold(True)

            # making font italic
            font.setItalic(True)

            # setting font to the painter
            painter.setFont(font)

            # drawing text
            painter.drawText(
                rect.topLeft() + QPoint(10, 10),
                "{}".format("Geek"),
            )

            # restoring the painter
            painter.restore()

下面是实现方法

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys

# QCalendarWidget Class
class Calendar(QCalendarWidget):

    def paintCell(self, painter, rect, date):
        super(Calendar, self).paintCell(painter, rect, date)

        # checking if date is selected date
        if date == self.selectedDate():

            # saving the painter
            painter.save()

            # creating a QFont object
            font = QFont()

            # setting pixel size of the font
            font.setPixelSize(11)

            # making font bold
            font.setBold(True)

            # making font italic
            font.setItalic(True)

            # setting font to the painter
            painter.setFont(font)

            # drawing text
            painter.drawText(
                rect.topLeft() + QPoint(10, 10),
                "{}".format("Geek"),
            )

            # restoring the painter
            painter.restore()



class Window(QMainWindow):

    def __init__(self):
        super().__init__()

        # setting title
        self.setWindowTitle("Python ")

        # setting geometry
        self.setGeometry(100, 100, 500, 400)

        # calling method
        self.UiComponents()

        # showing all the widgets
        self.show()


    # method for components
    def UiComponents(self):

        # creating a QCalendarWidget object
        # as Calendar class inherits QCalendarWidget
        self.calendar = Calendar(self)

        # setting cursor
        self.calendar.setCursor(Qt.PointingHandCursor)

        # setting size of the calendar
        self.calendar.resize(350, 240)

        # setting font to the calendar
        self.calendar.setFont(QFont('Times', 5))

        # move the calendar
        self.calendar.move(10, 10)



# create pyqt5 app
App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# start the app
sys.exit(App.exec())

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

PyQt5 日历控件QCalendarWidget