The question is: how best to make the user type a date in dateEdit, this date is read and the number indicating the day number in the year is written to the variable?

    1 answer 1

    You can do this:

    import sys from PyQt5 import QtCore, QtGui, QtWidgets class MyForm(QtWidgets.QDialog): def __init__(self): super().__init__() self.dateEdit = QtWidgets.QDateEdit(self) self.dateEdit.setDateTime(QtCore.QDateTime(QtCore.QDate(2019, 2, 23), QtCore.QTime(0, 0, 0))) self.dateEdit.setDate(QtCore.QDate(2019, 2, 23)) self.dateEdit.setDisplayFormat("dd/MM/yyyy") self.label = QtWidgets.QLabel() self.label.setText("{}".format(self.dateEdit.dateTime().toString('dd-MM-yyyy'))) self.button = QtWidgets.QPushButton('Run') self.button.clicked.connect(self.onClick) layout = QtWidgets.QVBoxLayout(self) layout.addWidget(self.label) layout.addWidget(self.dateEdit) layout.addWidget(self.button) def onClick(self): firstDayText = '{}-01-01'.format(self.dateEdit.dateTime().toString('yyyy')) firstDay = QtCore.QDateTime.fromString(firstDayText, "yyyy-MM-dd") numDay = firstDay.daysTo(self.dateEdit.dateTime()) self.label.setText("Выбрана дата {}, с начала года прошло {} дня/ей" "".format(self.dateEdit.dateTime().toString('dd-MM-yyyy'), numDay)) if __name__=="__main__": app = QtWidgets.QApplication(sys.argv) myapp = MyForm() myapp.show() sys.exit(app.exec_()) 

    enter image description here