这是我在本地系统中使用的同一个脚本。同一个脚本正在另一个位置使用,而该脚本的设置也是同一个系统。但不起作用。在
在这件事上我自己找不到什么不对的地方。你知道是不是Python的Bug?在
它所做的是全天候运行,并在上午9点之前和下午3点之后显示一个简单的图形用户界面(PC具有24小时时间模式,而不是AM或PM)。但从长远来看,这是行不通的。(在我的本地系统中,它做得很好)。我如何解决这个问题?在
import sys
import datetime
import time
from PyQt4 import QtCore, QtGui
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
flags = QtCore.Qt.Window
flags |= QtCore.Qt.FramelessWindowHint
flags |= QtCore.Qt.WindowStaysOnTopHint
self.setWindowFlags(flags)
self.b = QtGui.QPushButton("9/15 open", self, clicked=self.close)
self.c = QtGui.QLabel("", self)
def myRun(self):
while True:
time.sleep(2)
print "[Debug]: " + self.showNowHour() + " " + self.showNowMinute()
hour = int(self.showNowHour())
minute = int(self.showNowMinute())
if (hour>8 and hour<15):
print "is open"
self.hide()
else:
print "is close"
self.show()
def showNowHour(self):
now = datetime.datetime.now()
now = now.strftime("%H")
return now
def showNowMinute(self):
now = datetime.datetime.now()
return now.strftime("%M")
if __name__ == "__main__":
app=QtGui.QApplication(sys.argv)
myapp=Main()
myapp.setStyleSheet("background-color: rgb(85, 0, 0);")
thread = QtCore.QThread()
thread.run = lambda myapp=myapp: myapp.myRun()
thread.start()
sys.exit(app.exec_())
跟进:
^{pr2}$
不知怎么的,你对strftime()的调用失败了。我看不出有什么不对,但这是唯一合乎逻辑的结论。但我不知道为什么要使用strftime,因为可以直接从datetime对象访问hour。在
尝试用'日期时间。日期时间。现在().小时'。它返回24小时范围内的整数
见http://docs.python.org/library/datetime.html#datetime-objects
这可能会失败,因为您正在从另一个线程调用GUI方法。您应该始终在主线程(也称为“GUI线程”)内执行GUI调用,否则很容易遇到问题。在
不要让线程在每个循环中休眠2秒,而是用每2秒触发一次的
QTimer
。把这样的东西放在你的主线上:在您的
^{pr2}$Main
类中,将myRun
方法替换为:不需要线程不是很好吗!在
相关问题 更多 >
编程相关推荐