QDateTimeEdit¶
QDateTimeEdit widget helps users select and edit date and time.
Let’s create a QDateTimeEdit object in the example and set it to display current date and time.
For further details check QDateTimeEdit official document .
Example¶
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):
lbl = QLabel('QTimeEdit')
datetimeedit = QDateTimeEdit(self)
datetimeedit.setDateTime(QDateTime.currentDateTime())
datetimeedit.setDateTimeRange(QDateTime(1900, 1, 1, 00, 00, 00), QDateTime(2100, 1, 1, 00, 00, 00))
datetimeedit.setDisplayFormat('yyyy.MM.dd hh:mm:ss')
vbox = QVBoxLayout()
vbox.addWidget(lbl)
vbox.addWidget(datetimeedit)
vbox.addStretch()
self.setLayout(vbox)
self.setWindowTitle('QDateTimeEdit')
self.setGeometry(300, 300, 300, 200)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
QDateTimeEdit appears on the window.
Description¶
datetimeedit = QDateTimeEdit(self)
datetimeedit.setDateTime(QDateTime.currentDateTime())
datetimeedit.setDateTimeRange(QDateTime(1900, 1, 1, 00, 00, 00), QDateTime(2100, 1, 1, 00, 00, 00))
Use QDateTimeEdit class to make a date,time editing widget (datetimeedit).
Type QDateTime.currentDateTime() in the setDateTime method so that current time and date is displayed when the program runs.
SetDateTimeRange allows you to limit the range of time you can select.
datetimeedit.setDisplayFormat('yyyy.MM.dd hh:mm:ss')
Use the setDisplayFormat method to display time in the form ‘yyyy.MM.dd hh:mm:ss’.
vbox = QVBoxLayout()
vbox.addWidget(lbl)
vbox.addWidget(datetimeedit)
vbox.addStretch()
self.setLayout(vbox)
Use the vertical box layout to position the label and date,time editing widget vertically and set it as the layout of the entire widget.
Prev/Next
Prev : QTimeEdit
Next : QTextBrowser