Calendar within QLineEdit

1

I have a question about PyQt5. I try to have a date input, so that the user presses a box and opens a calendar to select the date.

In some places I have read about the code setCalendarPopup(true) , but I have not been able to use it successfully.

What I want is that when I click on a QlineEdit ( fin_entry in my code) a calendar opens ( cal in my code).

I attach my code:

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication,QLineEdit,QInputDialog,QFileDialog,QComboBox,QSpinBox,QDateEdit,QCalendarWidget,QLabel
from PyQt5.QtGui import QIcon

class ventana(QMainWindow):
    def __init__(self):
        super().__init__()
        self.nombre_ventana = "valorizador"
        self.ventana_izquierda = 100
        self.ventana_arriba = 100
        self.ventana_ancho=1440
        self.ventana_alto = 900


        self.initUI()

    def initUI(self):

        self.setGeometry(self.ventana_izquierda,self.ventana_arriba,self.ventana_ancho,self.ventana_alto)
        self.setWindowTitle(self.nombre_ventana)
        menu=self.menuBar()
        archivo=menu.addMenu("Archivo")
        boton_guardar=QAction("Guardar",self)      

        etiqueta_tasa= QLabel("Tasa Fija %",self)
        etiqueta_tasa.move(50,100)
        tasa_entry = QLineEdit(self)
        tasa_entry.setInputMask("9,99")
        tasa_entry.move(150, 100)

        etiqueta_moneda = QLabel("Moneda", self)
        etiqueta_moneda.move(50,150)
        moneda_combobox=QComboBox(self)
        valores_moneda=["USD","Otra"]
        moneda_combobox.move(150,150)

        for i in valores_moneda:
           moneda_combobox.addItem(i)

        etiqueta_fincontrato = QLabel("Fecha vencimiento", self)
        etiqueta_fincontrato.move(50, 250)
        fin_entry = QLineEdit(self)
        cal = QCalendarWidget(self) #aquì no se como agregarlo dentro del QlineEdit
        cal.move(400,400)
        fin_entry.setInputMask("99-99-9999")
        fin_entry.move(150, 250)

        self.show()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = ventana()
    sys.exit(app.exec_())
    
asked by Richie 12.12.2017 в 03:53
source

1 answer

1

You have to use QDateTimeEdit instead of QLineEdit, enable the calendarPopup, in addition to placing the appropriate format:

cal = QDateTimeEdit(self)
cal.setCalendarPopup(True)
cal.setDisplayFormat("dd-MM-yyyy")

Plus:

As you will notice the calendar is in English, so you must change the language for them we use setLocale ():

cal.calendarWidget().setLocale(QLocale(QLocale.Spanish))

I also recommend not using absolute positions, but layouts as I show below:

class ventana(QMainWindow):    
    def __init__(self):
        super().__init__()
        self.nombre_ventana = "valorizador"
        self.initUI()
        self.resize(640, 480)

    def initUI(self):
        self.setWindowTitle(self.nombre_ventana)
        menu=self.menuBar()
        archivo=menu.addMenu("Archivo")
        boton_guardar=QAction("Guardar",self)      

        widget = QWidget(self)
        self.setCentralWidget(widget)
        hlay = QHBoxLayout(widget)
        flay = QFormLayout()
        hlay.addItem(QSpacerItem(200, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
        hlay.addLayout(flay)
        hlay.addItem(QSpacerItem(200, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

        tasa_entry = QLineEdit(self)
        tasa_entry.setInputMask("9,99")
        moneda_combobox=QComboBox(self)
        valores_moneda=["USD","Otra"]
        moneda_combobox.addItems(valores_moneda)
        cal = QDateTimeEdit(self)
        cal.setCalendarPopup(True)
        cal.setDisplayFormat("dd-MM-yyyy")
        cal.calendarWidget().setLocale(QLocale(QLocale.Spanish))

        flay.addRow("Tasa Fija %", tasa_entry)
        flay.addRow("Moneda", moneda_combobox)
        flay.addRow("Fecha vencimiento", cal)

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

Exit:

    
answered by 12.12.2017 / 04:00
source