Python Forum
[PyQt] manage display date
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] manage display date
#1
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:

[Image: u0c9.png]

[Image: 7590.png]

[Image: g7vl.png]

incorrect display :

[Image: cvp8.png]

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
Reply
#2
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))
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] display the contents of the table in descending order of the date atlass218 14 6,889 Nov-18-2019, 03:20 PM
Last Post: Denni
  Display and update the label text which display the serial value jenkins43 5 8,992 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 3,835 Jan-05-2019, 01:48 PM
Last Post: wuf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020