from PyQt5.QtCore import QDate, Qt
from PyQt5.QtGui import (QColor, QFont, QTextCharFormat, QTextLength,
QTextTableFormat)
from PyQt5.QtWidgets import (QApplication, QComboBox, QDateTimeEdit,
QHBoxLayout, QLabel, QMainWindow, QSpinBox, QTextBrowser, QVBoxLayout,
QWidget)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.selectedDate = QDate.currentDate()
self.fontSize = 10
centralWidget = QWidget()
dateLabel = QLabel("日期:")
monthCombo = QComboBox()
for month in range(1, 13):
monthCombo.addItem(QDate.longMonthName(month))
yearEdit = QDateTimeEdit()
yearEdit.setDisplayFormat('yyyy')
yearEdit.setDateRange(QDate(1753, 1, 1), QDate(8000, 1, 1))
monthCombo.setCurrentIndex(self.selectedDate.month() - 1)
yearEdit.setDate(self.selectedDate)
self.fontSizeLabel = QLabel(" 字体大小:")
self.fontSizeSpinBox = QSpinBox()
self.fontSizeSpinBox.setRange(1, 64)
self.fontSizeSpinBox.setValue(7)
self.editor = QTextBrowser()
self.insertCalendar()
monthCombo.activated.connect(self.setMonth)
yearEdit.dateChanged.connect(self.setYear)
self.fontSizeSpinBox.valueChanged.connect(self.setfontSize)
controlsLayout = QHBoxLayout()
controlsLayout.addWidget(dateLabel)
controlsLayout.addWidget(monthCombo)
controlsLayout.addWidget(yearEdit)
controlsLayout.addSpacing(24)
controlsLayout.addWidget(self.fontSizeLabel)
controlsLayout.addWidget(self.fontSizeSpinBox)
controlsLayout.addStretch(1)
centralLayout = QVBoxLayout()
centralLayout.addLayout(controlsLayout)
centralLayout.addWidget(self.editor, 1)
centralWidget.setLayout(centralLayout)
self.setCentralWidget(centralWidget)
def insertCalendar(self):
self.editor.clear()
cursor = self.editor.textCursor()
cursor.beginEditBlock()
date = QDate(self.selectedDate.year(), self.selectedDate.month(), 1)
tableFormat = QTextTableFormat()
tableFormat.setAlignment(Qt.AlignHCenter)
tableFormat.setBackground(QColor('#E0E0E0'))
tableFormat.setCellPadding(2)
tableFormat.setCellSpacing(4)
constraints = [QTextLength(QTextLength.PercentageLength, 14),
QTextLength(QTextLength.PercentageLength, 14),
QTextLength(QTextLength.PercentageLength, 14),
QTextLength(QTextLength.PercentageLength, 14),
QTextLength(QTextLength.PercentageLength, 14),
QTextLength(QTextLength.PercentageLength, 14),
QTextLength(QTextLength.PercentageLength, 14)]
tableFormat.setColumnWidthConstraints(constraints)
table = cursor.insertTable(1, 7, tableFormat)
frame = cursor.currentFrame()
frameFormat = frame.frameFormat()
frameFormat.setBorder(1)
frame.setFrameFormat(frameFormat)
format = cursor.charFormat()
format.setFontPointSize(self.fontSize)
boldFormat = QTextCharFormat(format)
boldFormat.setFontWeight(QFont.Bold)
highlightedFormat = QTextCharFormat(boldFormat)
highlightedFormat.setBackground(Qt.yellow)
for weekDay in range(1, 8):
cell = table.cellAt(0, weekDay-1)
cellCursor = cell.firstCursorPosition()
cellCursor.insertText(QDate.longDayName(weekDay), boldFormat)
table.insertRows(table.rows(), 1)
while date.month() == self.selectedDate.month():
weekDay = date.dayOfWeek()
cell = table.cellAt(table.rows()-1, weekDay-1)
cellCursor = cell.firstCursorPosition()
if date == QDate.currentDate():
cellCursor.insertText(str(date.day()), highlightedFormat)
else:
cellCursor.insertText(str(date.day()), format)
date = date.addDays(1)
if weekDay == 7 and date.month() == self.selectedDate.month():
table.insertRows(table.rows(), 1)
cursor.endEditBlock()
self.setWindowTitle("日历")
def setfontSize(self, size):
self.fontSize = size
self.insertCalendar()
def setMonth(self, month):
self.selectedDate = QDate(self.selectedDate.year(), month + 1,
self.selectedDate.day())
self.insertCalendar()
def setYear(self, date):
self.selectedDate = QDate(date.year(), self.selectedDate.month(),
self.selectedDate.day())
self.insertCalendar()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.resize(640, 280)
window.show()
sys.exit(app.exec_())
python日历PyQt5实现日历 .zip
版权申诉
120 浏览量
2024-04-24
16:33:51
上传
评论
收藏 2KB ZIP 举报
m0_74456535
- 粉丝: 89
- 资源: 725
最新资源
- 大创项目编程示例开发案列优质学习资料资源工具与案列应用场景开发文档教程资料.txt
- 树莓派智能车编程实例开发案列优质学习资料资源工具与案列应用场景开发文档教程资料.txt
- 电子设计竞赛(Electronic Design Contest) 开发案列优质学习资料资源工具与案列应用场景开发文档教程资料
- Cpu-Z 是一款计算机的CPU检测软件
- 美国大学生数学建模竞赛 开发案列优质学习资料资源工具与案列应用场景开发文档教程资料.txt
- 最新版点微同城源码34.7+全套插件+小程序前后端附图片
- 计算机二级 开发案列优质学习资料资源工具与案列应用场景开发文档教程资料.txt
- Python 编程示例开发案列优质学习资料资源工具与案列应用场景开发文档教程资料.txt
- Java学生管理系统数列开发案列优质学习资料资源工具与案列应用场景开发文档教程资料.txt
- dysbb_121970.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈