氣輕PyQt5 12 日歷控件(QCalendarWidget)
?
在日歷控件選擇日期后,選擇的結(jié)果會(huì)顯示在文本框控件。
?
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt, QDate
import sys
import datetime
?
class PyQt512(QWidget):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt5')??????????????? # 設(shè)置窗口標(biāo)題
??????? self.resize(250, 220)?????????? ????????????????# 設(shè)置窗口大小
?
??????? dateData = datetime.datetime.now().date()
??????? dateLabel = dateData.strftime('%Y-%m-%d')
??????? self.edit = QLineEdit(self)???????????????????? # 輸入框
??????? self.edit.setGeometry(10, 0,230, 40)??????????? # 設(shè)置位置和大小
??????? self.edit.setText(dateLabel)
??????? self.edit.setReadOnly(True)???????????????????? # 只讀
??????? self.edit.setAlignment(Qt.AlignmentFlag.AlignHCenter)# 居中設(shè)置
??????? self.edit.setStyleSheet('background-color:#FFE4B5;color : #32CD32; \
???????????? ???????font: bold italic large /"Times New Roman/";font-size:25px')
?
??????? cal = QCalendarWidget(self)
??????? cal.setGridVisible(True)
??????? cal.setGeometry(10, 60,230, 140)??????????? # 設(shè)置位置和大小
??????? cal.clicked[QDate].connect(self.updateDate)
?
?? ?????self.show()
?
??? def updateDate(self,date):
??????? self.edit.setText(date.toString("yyyy-MM-dd"))
?
if __name__ == '__main__':
??? app = QApplication(sys.argv)
??? window = PyQt512()
??? sys.exit(app.exec_())
?
執(zhí)行結(jié)果
