[PyQt] manage display date - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] manage display date (/thread-22406.html) |
manage display date - atlass218 - Nov-11-2019 I am trying to create a desktop application with QtDesigner and pyqt5. I created a Groupbox with QtDesigner in which I put 03 lineEdit, successively for the day, month and year, a button to activate a function and a linedit with readonly option to display the complete introduced date. Here are some pictures that reflect the result of my code: the first 03 pictures show a correct date but the 4th picture shows an incorrect date (the day is past 31 and the month is past 12) correct display: incorrect display : code python : def insert_date(self) : if (10<= int(self.lineEdit_day.text())<= 31 ) or (len(self.lineEdit_day.text())>1) : day_of_date=self.lineEdit_day.text() elif (1<=int(self.lineEdit_day.text())<=9 ) : day_of_date='0'+self.lineEdit_day.text() else: self.textEdit_error_display.setText("invalid fields") if (9<int(self.lineEdit_month.text() )<13) or (len(self.lineEdit_month.text())>1) : month_of_date=self.lineEdit_month.text() elif (1<=int(self.lineEdit_month.text() )<=9) : my_month='0'+self.lineEdit_month.text() else: self.textEdit_error_display.setText("invalid fields") year_of_date=self.lineEdit_year.text() self.lineEdit_date_introduced.setText(day_of_date + '/'+month_of_date+'/'+ year_of_date)thanks for help RE: manage display date - Gribouillis - Nov-11-2019 The built-in module datetime is always helpful when handling dates and time import datetime as dt def insert_date(self): try: d = int(self.lineEdit_day.text()) m = int(self.lineEdit_month.text()) y = int(self.lineEdit_year.text()) date = dt.date(y, m, d) except ValueError: self.textEdit_error_display.setText("invalid fields") else: self.lineEdit_date_introduced.setText("{:%d/%m/%Y}".format(date)) RE: manage display date - atlass218 - Nov-11-2019 thanks for answer. I have testing the code. It's working for me but : when I write an incorrect date I have an error dispalyed at textEdit_error_display if then I correct the date entered previously; the error message displays on textEdit_error_display, does not automatically clear to solve this problematic I added this line of code : self.textEdit_error_display.setText("")the complete code is : import datetime as dt def insert_date(self): try: d = int(self.lineEdit_day.text()) m = int(self.lineEdit_month.text()) y = int(self.lineEdit_year.text()) date = dt.date(y, m, d) except ValueError: self.textEdit_error_display.setText("invalid fields") else: self.lineEdit_date_introduced.setText("{:%d/%m/%Y}".format(date)) self.textEdit_error_display.setText("")thanks for the code that helped me find a solution to my problem |