生命不是要超越别人,而是要超越自己。每天醒来并告诉自己:更少的理由,更大的腹部,更甜的嘴,更小的脾气,更快的动作,更高的效率,一点微笑和脑。一站式
QCalendarWidget 是日历控件。它允许用户以简单和直观的方式选择日期。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
PyQt5 好代码教程
这个例子使用QCalendarWidget控件创建了一个日历。
作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月4日
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QLabel
from PyQt5.QtCore import QDate
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.move(20, 20)
cal.clicked[QDate].connect(self.showDate)
self.lb1 = QLabel(self)
date = cal.selectedDate()
self.lb1.setText(date.toString())
self.lb1.move(130, 260)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('日历控件')
self.show()
def showDate(self, date):
self.lb1.setText(date.toString())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
这个例子有一个日历控件和一个标签控件。当前选定的日期用标签显示。
cal = QCalendarWidget(self)
使用 QCalendarWidget 创建日历控件
cal.clicked[QDate].connect(self.showDate)
如果我们在日历控件中选择一个日期,clicked[QDate]信号将连接到用户定义的showDate()方法。
def showDate(self, date):
self.lb1.setText(date.toString())
我们通过调用selectedDate()方法检索选定的日期。然后我们将Date对象转换成字符串并显示在标签控件中。
程序执行后
以上就是PyQt5每天必学之日历控件QCalendarWidget。贤从智中取,智从学中求。更多关于PyQt5每天必学之日历控件QCalendarWidget请关注haodaima.com其它相关文章!