凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

26. PyQt6 QCalendarWidget

2023-10-17 python 252
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget, QLabel, QVBoxLayout, QWidget
from PyQt6.QtGui import QGuiApplication
from PyQt6.QtCore import QDate


class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.lbl = None
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt6 QCalendarWidget")

        vbox = QVBoxLayout()

        cal = QCalendarWidget()
        cal.setGridVisible(True)
        cal.clicked[QDate].connect(self.showDate)

        vbox.addWidget(cal)

        self.lbl = QLabel(self)
        date = cal.selectedDate()
        self.lbl.setText(date.toString())

        vbox.addWidget(self.lbl)

        w = QWidget()
        w.setLayout(vbox)

        self.setCentralWidget(w)

        self.setGeometry(100, 100, 400, 300)
        self.center()

    def showDate(self, date):
        self.lbl.setText(date.toString())

    def center(self):
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    cls_app = App()
    cls_app.show()
    sys.exit(app.exec())

image.png

文章评论

0条评论