Wednesday, October 12, 2022

Date Edit Box with calendar Dropdown in PyQt6

 This demo program shows how to create a date edit input box with a calendar dropdown and place at certain location in the window.


The output:


The code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import sys
import os
from PyQt6.QtWidgets import QApplication,  QWidget, QDateEdit, QLabel
from PyQt6.QtCore import  QDate
from PyQt6 import QtCore, QtGui

class Window(QWidget):

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

    def initUI(self):
        self.date_edit = QDateEdit(self, date=QDate.currentDate(), calendarPopup=True)
        self.date_edit.setGeometry(25, 25, 150, 40)
        self.date_edit.dateChanged.connect(self.update)
        self.result_label = QLabel('', self)
        self.result_label.setGeometry(250, 25, 150, 40)
        self.setGeometry(25, 45, 350, 150)
        self.setWindowTitle('Qdateedit Tutorial')
        self.show()
    def update(self):
        value = self.date_edit.date()
        
        self.result_label.setText(str(value.toPyDate()))    
def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()


No comments:

Post a Comment