问题描述
“ sunset_time = time(18,30,00)”行产生一个'QTime'对象,不可调用错误...。 我在做什么错?..我的应用程序应该获取并显示当前时间,然后设置日落时间,然后从日落时间中减去当前时间,以便获取并显示“距离日落还剩几分钟”
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,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
解决方法
我不了解执行以下操作的逻辑:
time = QtCore.QTime.currentTime()
# ...
sunset_time = time(18,30,00)
正确的做法是从日落创建QTime并将其“减去”,这等效于知道可以通过secsTo方法获得的这些QTime之间的时间:
def showlcd_2():
current_time = QtCore.QTime.currentTime()
sunset_time = QtCore.QTime(18,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"))
,
发生错误是因为无法调用实例化的QTime
对象time
本身。
在下面的语句sunset_time = time(18,00)
中,您将调用带有三个参数的实例化Qtime对象:18,00。但是您只能在构造函数上使用此方法,而正确使用的方法是:sunset_time = QtCore.QTime(18,00)
因此,要回答您的问题,则会发生错误,因为您调用了实例化的对象而不是其构造函数。
通常,请查看其他答案以获取有关您的解决方案的提示:)