氣輕PyQt6 12日歷控件(QCalendarWidget)
?
在日歷控件選擇日期后,選擇的結(jié)果會(huì)顯示在文本框控件。
?
from PyQt6 import QtCore, QtGui
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QPalette, QFont, QColor
from PyQt6.QtCore import Qt, QDate
import sys
import datetime
?
class PyQt612(QWidget):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt6')??????????????? # 設(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 /"SimSun/";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 = PyQt612()
??? sys.exit(app.exec())
?
執(zhí)行結(jié)果
