QDateTimeEdit 사용법
반응형
import sys
from PyQT5.QtWidgets import QApplication, QWidget, QLabel, QDateTimeEdit, QVBoxLayout
from PyQT5.QtCore import QDateTime


class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.lbl = QLabel('QTimeEdit')

        self.lbl2 = QLabel('QTimeEdit')

        self.datetimeedit = QDateTimeEdit(self)
        self.datetimeedit.setDateTime(QDateTime.currentDateTime())
        self.datetimeedit.setDateTimeRange(QDateTime(1900, 1, 1, 00, 00, 00), QDateTime(2100, 1, 1, 00, 00, 00))
        self.datetimeedit.setDisplayFormat('yyyy.MM.dd hh:mm:ss')

        self.datetimeedit.dateTimeChanged.connect(lambda: self.dt_method())

        vbox = QVBoxLayout()
        vbox.addWidget(self.lbl)
        vbox.addWidget(self.lbl2)
        vbox.addWidget(self.datetimeedit)
        vbox.addStretch()

        self.setLayout(vbox)

        self.setWindowTitle('QDateTimeEdit')
        self.show()

    # method called by the datetime
    def dt_method(self):
        # getting current datetime
        value = self.datetimeedit.dateTime()

        # setting text to the label
        self.lbl2.setText("New DateTime : " + str(value))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

 

 

 

PyQT5 : https://doc.qt.io/qtforpython/PySide6/QtWidgets/QDateTimeEdit.html?highlight=qdatetimeedit

 

QDateTimeEdit - Qt for Python

Previous QDateEdit

doc.qt.io


추가로 Python에서의 datetime 포맷법

from datetime import datetime

# 파이썬 날짜-시간 포맷
print(datetime.now().strftime('%F %T.%f'))
# ms단위를 3자릿까지만
print(datetime.now().strftime('%F %T.%f')[:-3])

이거만 알면? 외우면? 될듯 하다...............

 

반응형