本文介绍了为 PyQt5 QCalendarWidget 单元格着色并在单元格内打印数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据要求,我需要创建一个用于任务管理的桌面应用程序.要求是以类似于 Outlook 日历的类似日历的格式显示所有计划的活动.该应用程序还将有一些其他按钮,用于根据日历上的预定活动将用户重定向到相应的 URL.

As per requirement, I need to create a desktop application for task management. The requirement is to show all the planned activities in a calendar like format, similar to the outlook calendar. The application will also have few other push buttons for redirecting users to the respective URLs on they are clicked based on the scheduled activities on the calendar.

我正在使用 python 的 PyQt5 模块开发这个桌面应用程序.我已经创建了所需的按钮和任务管理,我已经使用 QCalendarWidget 创建了日历.我还可以访问单个单元格,并能够根据日历上使用的点击次数打印日期.

I'm developing this desktop application using python's PyQt5 module. I have created the required push button and for task management, I have created the calendar using QCalendarWidget. I'm also able to access the individual cells and able to print the date based on used clicks on the calendar.

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMenuBar, QAction, QDialog, QGroupBox, QHBoxLayout, QVBoxLayout, QGridLayout, QLabel, QCalendarWidget, QMainWindow
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QRect, QDate
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QSize


class Window(QDialog):
        def __init__(self):
                super().__init__()
                win = QWidget()
                self.title = "Calendar Application"
                self.top = 100
                self.left = 100
                self.width = 400
                self.height = 400
                self.InitWindow()

        def InitWindow(self):
                #self.setWindowIcon(QtGui.QIcon("heading.png"))
                self.setWindowTitle(self.title)
                self.setGeometry(self.top, self.left, self.width, self.height)
                self.createMenu()
                self.createHeader()
                self.createHomeLayout()
                self.show()


        def createHeader(self):
                gboxLayout = QGridLayout()
                self.label = QLabel("Calendar Application")
                gboxLayout.addWidget(self.label, 1, 1)
                self.header_groupBox = QGroupBox()
                self.header_groupBox.setLayout(gboxLayout)

        def createMenu(self):
                mainMenu = QMenuBar()
                fileMenu = mainMenu.addMenu("File")
                helpMenu = mainMenu.addMenu("Help")

                exitAction = QAction('Exit', self)
                helpAction = QAction('About Us', self)

                fileMenu.addAction(exitAction)
                helpMenu.addAction(helpAction)

        def createHomeLayout(self):
### --------------------------- ACTIVITY Widget --------------------------------- ###

                hbox = QHBoxLayout()
                self.activity_groupBox = QGroupBox("Activities")
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.activity_groupBox.setFont(font)
                self.gridLayout = QGridLayout()

                self.button1 = QPushButton("Activity-1", self)
                self.button1.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button1.setFont(font)
                self.gridLayout.addWidget(self.button1, 0, 0)

                self.button2 = QPushButton("Activity-2", self)
                self.button2.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button2.setFont(font)
                self.gridLayout.addWidget(self.button2, 0, 1)

                self.button3 = QPushButton("Activity-3", self)
                self.button3.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button3.setFont(font)
                self.gridLayout.addWidget(self.button3, 0, 2)

                self.button4 = QPushButton("Activity-4", self)
                self.button4.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button4.setFont(font)
                self.gridLayout.addWidget(self.button4, 1, 0)

                self.button5 = QPushButton("Activity-5", self)
                self.button5.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button5.setFont(font)
                self.gridLayout.addWidget(self.button5, 1, 1)

                self.button6 = QPushButton("Activity-6", self)
                self.button6.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button6.setFont(font)
                self.gridLayout.addWidget(self.button6, 1, 2)

                self.button7 = QPushButton("Activity-7", self)
                self.button7.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button7.setFont(font)
                self.gridLayout.addWidget(self.button7, 2, 0)

                self.button8 = QPushButton("Activity-8", self)
                self.button8.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button8.setFont(font)
                self.gridLayout.addWidget(self.button8, 2, 1)

                self.button9 = QPushButton("Activity-9", self)
                self.button9.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button9.setFont(font)
                self.gridLayout.addWidget(self.button9, 2, 2)

                self.button10 = QPushButton("Activity-10", self)
                self.button10.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button10.setFont(font)
                self.gridLayout.addWidget(self.button10, 3, 0)

                self.button11 = QPushButton("Activity-11", self)
                self.button11.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button11.setFont(font)
                self.gridLayout.addWidget(self.button11, 3, 1)

                self.button12 = QPushButton("Activity-12", self)
                self.button12.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button12.setFont(font)
                self.gridLayout.addWidget(self.button12, 3, 2)

                self.button13 = QPushButton("Activity-13", self)
                self.button13.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button13.setFont(font)
                self.gridLayout.addWidget(self.button13, 4, 0)

                self.button14 = QPushButton("Activity-14", self)
                self.button14.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button14.setFont(font)
                self.gridLayout.addWidget(self.button14, 4, 1)

                self.button15 = QPushButton("Activity-15", self)
                self.button15.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button15.setFont(font)
                self.gridLayout.addWidget(self.button15, 4, 2)

                self.button16 = QPushButton("Activity-16", self)
                self.button16.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button16.setFont(font)
                self.gridLayout.addWidget(self.button16, 5, 0)

                self.button17 = QPushButton("Activity-17", self)
                self.button17.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button17.setFont(font)
                self.gridLayout.addWidget(self.button17, 5, 1)

                self.button18 = QPushButton("Activity-18", self)
                self.button18.setMinimumHeight(40)
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.button18.setFont(font)
                self.gridLayout.addWidget(self.button18, 5, 2)

                self.activity_groupBox.setLayout(self.gridLayout)
                #self.report_groupBox.setLayout(gridLayout)


### ------------------------ REPORTS Widget -------------------------------- ###

                self.report_groupBox = QGroupBox("Reports")
                font = QtGui.QFont()
                font.setFamily("Verdana")
                font.setBold(True)
                self.report_groupBox.setFont(font)

                vbox = QVBoxLayout(self)

                cal = QCalendarWidget(self)
                cal.setGridVisible(True)
                cal.clicked[QDate].connect(self.showDate)
                vbox.addWidget(cal)

                self.lbl = QLabel(self)
                date = cal.selectedDate()
                self.lbl.setText(date.toString("yyyy-MM-dd"))
                vbox.addWidget(self.lbl)

                hbox.addWidget(self.activity_groupBox)
                hbox.addWidget(self.report_groupBox)
                self.setLayout(hbox)

        def close(self):
                self.close()

        #def recruitWindow(self):
        def showDate(self, date):
                self.lbl.setText(date.toString("yyyy-MM-dd"))


if __name__ == '__main__':
    App = QApplication(sys.argv)
    window = Window()
    sys.exit(App.exec())

现在的要求是访问日历的每个单元格,如果有任何活动时间表,并在单元格中提及活动名称,则将其突出显示,类似于 Outlook 日历.所有活动名称和时间表都将作为 Excel 表格中的输入给出.我可以访问日历单元格,但不确定如何突出显示单元格并在单元格中打印计划的活动.

The requirement now is to access each cell of the calendar and highlight it if there is any activity schedule and also mention the activity name in the cell, similar to the outlook calendar. All the activity name and schedule will be given as input in an excel sheet. I'm able access the calendar cells but not sure how to highlight the cells and print the scheduled activity in the cell.

这可能吗?我是在正确的轨道上还是应该考虑其他一些方法?

Is that possible? Am I on the right track or should I think of some other ways?

可以从此链接访问 QCalendar.

The QCalendar can be accessed with from this link.

推荐答案

Subclass QCalendarWidget 并重新定义方法 paintCell().

Subclass QCalendarWidget and redefine the method paintCell().

例如,要在您有事件的每个单元格中打印一个红点:

For example, to print a red dot in each cell where you have an event:

class Scheduler(QCalendarWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.events = {
            QDate(2019, 5, 24): ["Bob's birthday"],
            QDate(2019, 5, 19): ["Alice's birthday"]
        }

    def paintCell(self, painter, rect, date):
        super().paintCell(painter, rect, date)
        if date in self.events:
            painter.setBrush(Qt.red)
            painter.drawEllipse(rect.topLeft() + QPoint(12, 7), 3, 3)

这篇关于为 PyQt5 QCalendarWidget 单元格着色并在单元格内打印数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:59