气轻PyQt6 12日历控件(QCalendarWidget)

2023-03-06 07:49:32 [来源:哔哩哔哩]

在日历控件选择日期后,选择的结果会显示在文本框控件。


(相关资料图)

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())

执行结果

x 广告
x 广告
© 2021 南极早报网 版权所有,备案号:粤ICP备2022077823号-13邮箱: 317 493 128@qq.com