I am not able to make datepicker in pyqt5. I am using calendarWidget and it working fine now. But i want dropdown datepicker in my menu bar and want to show selected date in lineEdit.
I have created a layout in QDesigner and adding 'DateEdit" widget. But i want same exactly as image shown.
I searched for datepicker and getting this link :How to add Today Button in QDateEdit Pop-up QCalendarWidget
I tried many approaches but it's not working.
Note: when i run my file dateEdit widget always show the Date:[01-01-2000]. it not showing the current date.
datepicker.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame_2)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.frame = QtWidgets.QFrame(self.frame_2)
self.frame.setStyleSheet("background-color: rgb(56, 122, 179);")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.frame)
self.label.setStyleSheet("color: rgb(243, 243, 243);")
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.dateEdit = QtWidgets.QDateEdit(self.frame)
self.dateEdit.setObjectName("dateEdit")
self.horizontalLayout.addWidget(self.dateEdit)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.verticalLayout_2.addWidget(self.frame)
self.tableWidget = QtWidgets.QTableWidget(self.frame_2)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.verticalLayout_2.addWidget(self.tableWidget)
self.verticalLayout.addWidget(self.frame_2)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Selected Date is :________________________"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The QDateEdit
already provides a QCalendarWidget so you only need to enable the calendarPopup
property:
import sys
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.dateedit = QtWidgets.QDateEdit(calendarPopup=True)
self.menuBar().setCornerWidget(self.dateedit, QtCore.Qt.TopLeftCorner)
self.dateedit.setDateTime(QtCore.QDateTime.currentDateTime())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
For PyQt6 I had to slightly modify eyllanesc's code. Here's what worked for in in PyQt6. Changes to the original are placed below the commented out code.
import sys
from PyQt6 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.dateedit = QtWidgets.QDateEdit(calendarPopup=True)
# self.menuBar().setCornerWidget(self.dateedit, QtCore.Qt.TopLeftCorner)
self.menuBar().setCornerWidget(self.dateedit, QtCore.Qt.Corner.TopLeftCorner)
self.dateedit.setDateTime(QtCore.QDateTime.currentDateTime())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
# sys.exit(app.exec_())
sys.exit(app.exec())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With