在日历控件选择日期后,选择的结果会显示在文本框控件。
(相关资料图)
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') # 设置窗口标题
self.resize(250, 220) # 设置窗口大小
dateData = datetime.datetime.now().date()
dateLabel = dateData.strftime('%Y-%m-%d')
self.edit = QLineEdit(self) # 输入框
self.edit.setGeometry(10, 0,230, 40) # 设置位置和大小
self.edit.setText(dateLabel)
self.edit.setReadOnly(True) # 只读
self.edit.setAlignment(Qt.AlignmentFlag.AlignHCenter)# 居中设置
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) # 设置位置和大小
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())
执行结果