从 qcalendar 获取数据

Fetch data from qcalendar

如何从 Qcalendar 获取数据。例如,当 select 21/10/2019 时,当我单击 "ok" 按钮时 "Monday" 将被提取

以下是我的代码:

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QCalendarWidget

class Ui_Form(QMainWindow):
    def __init__(self):
        self.calendarWidget = QtWidgets.QCalendarWidget(Form)
        self.label = QtWidgets.QLabel(Form)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton_2 = QtWidgets.QPushButton(Form)

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(700, 700)
        self.calendarWidget.setGeometry(QtCore.QRect(100, 110, 510, 454))
        self.calendarWidget.setMinimumSize(QtCore.QSize(200, 144))
        self.calendarWidget.setObjectName("calendarWidget")
        self.label.setGeometry(QtCore.QRect(100, 50, 101, 31))
        self.label.setObjectName("label")
        self.pushButton.setGeometry(QtCore.QRect(460, 600, 76, 37))
        self.pushButton.setObjectName("backButton")
        self.pushButton_2.setGeometry(QtCore.QRect(560, 600, 76, 37))
        self.pushButton_2.setObjectName("okbutton")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        selectionMode()


    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Select date"))
        self.label.setText(_translate("Form", "Select date"))
        self.pushButton.setText(_translate("Form", "Back"))
        self.pushButton_2.setText(_translate("Form", "ok"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

void QCalendarWidget::clicked(const QDate &date)

This signal is emitted when a mouse button is clicked. The date the mouse was clicked on is specified by date. The signal is only emitted when clicked on a valid date, e.g., dates are not outside the minimumDate() and maximumDate(). If the selection mode is NoSelection, this signal will not be emitted.

QString QDate::toString(const QString &format) const

Returns the date as a string. The format parameter determines the format of the result string. https://doc.qt.io/qt-5/qdate.html#toString-2

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QCalendarWidget, QWidget


# WARNING! All changes made in this file will be lost!
class Ui_Form(object):
    '''
        # WARNING! All changes made in this file will be lost !!!!!!

    class Ui_Form(QMainWindow):
        def __init__(self):
            self.calendarWidget = QtWidgets.QCalendarWidget(Form)
            self.label = QtWidgets.QLabel(Form)
            self.pushButton = QtWidgets.QPushButton(Form)
            self.pushButton_2 = QtWidgets.QPushButton(Form)
    '''

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(700, 700)

        self.calendarWidget = QtWidgets.QCalendarWidget(Form)
        self.calendarWidget.setGeometry(QtCore.QRect(100, 110, 510, 454))
        self.calendarWidget.setMinimumSize(QtCore.QSize(200, 144))
        self.calendarWidget.setObjectName("calendarWidget")

        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(100, 50, 101, 31))
        self.label.setObjectName("label")

        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(460, 600, 76, 37))
        self.pushButton.setObjectName("backButton")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(560, 600, 76, 37))
        self.pushButton_2.setObjectName("okbutton")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
# ?       selectionMode()


    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Select date"))
        self.label.setText(_translate("Form", "Select date"))
        self.pushButton.setText(_translate("Form", "Back"))
        self.pushButton_2.setText(_translate("Form", "ok"))


class MyWindow(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super(MyWindow, self).__init__()

        self.setupUi(self)

        self.calendarWidget.setGridVisible(True)
        self.calendarWidget.clicked[QtCore.QDate].connect(self.showDate)
        self.date = self.calendarWidget.selectedDate()
        self.label.setText(self.date.toString("dd-MM-yyyy dddd")) 

        self.pushButton_2.clicked.connect(self.clickedOk)

    def showDate(self, date): 
#        self.label.setText(date.toString("dd-MM-yyyy dddd"))     # ! Try to uncomment ! 
        self.date = date

    def clickedOk(self):
        self.label.setText(self.date.toString("dd-MM-yyyy dddd"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
#    Form = QtWidgets.QWidget()
#    ui = Ui_Form()
#    ui.setupUi(Form)
#    Form.show()
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())