PQt QDateEdit
QDateEdit
The QDateEdit widget is used to let the user select and edit dates. In the example, let's create one QDateEdit object and set it to appear as the current date.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDateEdit, QVBoxLayout
from PyQt5.QtCore import QDate
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
lbl = QLabel('QDateEdit')
dateedit = QDateEdit(self)
dateedit.setDate(QDate.currentDate())
dateedit.setMinimumDate(QDate(1900, 1, 1))
dateedit.setMaximumDate(QDate(2100, 12, 31))
# dateedit.setDateRange(QDate(1900, 1, 1), QDate(2100, 12, 31))
vbox = QVBoxLayout()
vbox.addWidget(lbl)
vbox.addWidget(dateedit)
vbox.addStretch()
self.setLayout(vbox)
self.setWindowTitle('QDateEdit')
self.setGeometry(300, 300, 300, 200)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
The Edit Date widget (QDateEdit) appears in the window.
Related course: Create Desktop Apps with Python PyQt5
Description
dateedit = QDateEdit(self)
dateedit.setDate(QDate.currentDate())
dateedit.setMinimumDate(QDate(1900, 1, 1))
dateedit.setMaximumDate(QDate(2100, 12, 31))
Use the QDateEdit class to create a date editing widget.
Type QDate.currentDate() in the setDate method so that it appears as the current date when the program runs.
SetMinimumDate and setMaximumDate allow you to limit the range of dates you can choose from.
The minimum date is set by default to September 14, 1752, and the maximum date is set to December 31, 9999.
The minimum date must be at least January 1, 100.
# dateedit.setDateRange(QDate(1900, 1, 1), QDate(2100, 12, 31))
The setDateRange method is the same as using setMinimumDate and setMaximumDate at the same time.
vbox = QVBoxLayout()
vbox.addWidget(lbl)
vbox.addWidget(dateedit)
vbox.addStretch()
self.setLayout(vbox)
Use the vertical box layout to place the Label and Date Edit widgets vertically and set them to the layout of the entire widget.
Related course: Create Desktop Apps with Python PyQt5