>

Qcalendar에서 데이터를 가져 오는 방법 예를 들어, 2019 년 10 월 21 일을 선택하면 "확인"버튼을 클릭하면 "월요일"이 가져옵니다

다음은 내 코드입니다 :

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


  • 답변 # 1

    와이즈 비즈

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

관련 자료

  • 이전 .net - C # 날짜 시간 형식의 작은 따옴표는 무엇을 의미합니까?
  • 다음 pandas - 데이터 프레임 열의 값을 다른 데이터 프레임의 올바른 행과 일치시키는 방법은 무엇입니까?