1 """ 2 QCalendarWidget:提供了日历插件 3 Author:dengyexun 4 DateTime:2018.11.22 5 """ 6 from PyQt5.QtWidgets import QWidget, QCalendarWidget, QApplication, QLabel, QVBoxLayout 7 from PyQt5.QtCore import QDate 8 import sys 9 10 class Example(QWidget): 11 12 def __init__(self): 13 super().__init__() 14 15 self.initUI() 16 17 def initUI(self): 18 # 将多个控件加入到qt的布局管理器中。建立一个箱布局 19 vbox = QVBoxLayout(self) 20 21 # # 日历对象,网格可见 22 cal = QCalendarWidget(self) 23 cal.setGridVisible(True) 24 # 点击日历,传入QDate数据,同showDate函数相关联 25 cal.clicked[QDate].connect(self.showDate) 26 27 # 把日历加入到这个箱子中 28 vbox.addWidget(cal) 29 30 # 显示选定的日期的label 31 self.lbl = QLabel(self) 32 date = cal.selectedDate() 33 self.lbl.setText(date.toString()) 34 35 # 把label也加入到这个箱子中 36 vbox.addWidget(self.lbl) 37 38 # 要给vbox设置布局 39 self.setLayout(vbox) 40 41 self.setGeometry(300, 300, 350, 300) 42 self.setWindowTitle('Calendar') 43 self.show() 44 45 def showDate(self, date): 46 """ 47 显示选中的日期 48 :param date: 点击日历组件,接收传入的参数 49 :return: 50 """ 51 self.lbl.setText(date.toString()) 52 53 54 if __name__ == '__main__': 55 app = QApplication(sys.argv) 56 ex = Example() 57 sys.exit(app.exec_())