PyQT4 QTime object is not callable

120 Views Asked by At

The "sunset_time = time(18,30,00)" line yields a 'QTime' object is NOT callable error.... what am I doing wrong?..My application is supposed to get and display current time then set sunset time and then subtract current time from sunset time in order to get and display "minutes left until sunset"

    timer = QtCore.QTimer(self)
    time= QtCore.QTime.currentTime()
    timer.timeout.connect(self.showlcd)
    timer.timeout.connect(self.showlcd_2)
    timer.start(1000)
    self.showlcd()
      

  def showlcd(self):
    time = QtCore.QTime.currentTime()
    current = time.toString('hh:mm')
    self.ui.lcdNumber.display(current)

  def showlcd_2(self):
    time = QtCore.QTime.currentTime()
    sunset = time.toString('18:30')
    current_time =(time.hour,time.minute,time.second)
    sunset_time = time(18,30,00)
    TillSunset = sunset_time-current_time
    minutesTillSunset=divmod(TillSunset.seconds, 60)
    self.ui.lblTillSunset.setText("minutesTillSunset.%s" %minutesTillSunset)
    self.ui.lcdNumber_2.display(sunset)

  def showTimeTillSunset(self):
    self.ui.lblTillSunset.display(TillSunset)
    pixmapTwo = QPixmap(os.getcwd() + '/sunset.jpg')
    lblSunsetPic.setPixmap(pixmapTwo)
    lblSunsetPic.show
2

There are 2 best solutions below

0
On

The error occurs because the instantiated QTime object time itself cannot be called. In the following statement sunset_time = time(18,30,00) you call an instantiated Qtime object with three arguments: 18,30,00. But you can only use this on the constructor, what you do correctly with: sunset_time = QtCore.QTime(18, 30, 00)
So to answer your question, the error occurs because you call an instantiated object instead of its constructor. Check out the other answer for tips on your solution in general :)

0
On

I don't understand the logic of doing the following:

time = QtCore.QTime.currentTime()
# ...
sunset_time = time(18,30,00)

The correct thing is to create QTime from the sunset and "subtract" it, which is equivalent to knowing the time between those QTimes that can be obtained through the secsTo method:

def showlcd_2():
    current_time = QtCore.QTime.currentTime()
    sunset_time = QtCore.QTime(18, 30, 00)

    m, s = divmod(current_time.secsTo(sunset_time), 60)

    self.ui.lblTillSunset.setText(("minutesTillSunset.%s" % m)
    self.ui.lcdNumber_2.display(sunset_time.toString("hh:mm"))