根据要求,我需要创建一个用于任务管理的桌面应用程序。要求以类似于Outlook日历的日历格式显示所有计划的活动。该应用程序还将具有其他几个按钮,用于根据日历上的计划活动将用户重定向到单击的相应URL。
我正在使用python的PyQt5模块开发此桌面应用程序。我已经创建了所需的按钮,并且对于任务管理,我已经使用QCalendarWidget创建了日历。我还可以访问各个单元格,并可以根据日历上的点击次数打印日期。
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工作表中作为输入给出。我可以访问日历单元,但不确定如何突出显示单元并在单元中打印计划的活动。
有可能吗?我是在正确的轨道上还是应该考虑其他方法?
可以从此链接访问QCalendar。
答案 0 :(得分:0)
子类QCalendarWidget
并重新定义方法paintCell()
。
例如,在有事件的每个单元格中打印一个红点:
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)
答案 1 :(得分:0)
我知道这是一个古老的问题,但是当我遇到类似的问题时却被引导至该问题,但提供的答案无效,我设法获得了该问题的答案以帮助我。我认为这可能对需要类似问题的人有所帮助。
这是我得到的代码:
connection = sqlite3.connect("YourDatabase.db")
crsr = connection.cursor()
crsr.execute("""SELECT Date FROM CalendarTable""")
date_format = "dd/MM/yyyy"
cell_format = QtGui.QTextCharFormat()
cell_format.setBackground(QtGui.QColor("lightblue"))
for row in crsr.fetchall():
date_str, *_ = row
dt = QtCore.QDate.fromString(date_str, date_format)
if dt.isValid():
self.calendarWidget.setDateTextFormat(dt, cell_format)
else:
self.DateInfoOutput.setText("{} does not match format {}".format(date_str, date_format))
要在程序中使用此代码,您需要创建一个如下数据库: The Database
然后将输出如下: The Final Output
我还将链接我的问题,以查看完整的代码以进行最终输出。 How to Output sql data onto a QCalendarWidget