这与我在本地系统中使用的脚本完全相同。相同的脚本正在另一个位置使用,它也是相同的系统相同的设置。但不工作。
我自己在这方面找不到任何问题。知道它是否是 Python 错误吗?
它的运行时间为 24/7,并在上午 9 点之前和下午 3 点之后显示一个简单的 Gui(PC 有 24 小时时间模式,不是上午或下午)。但从长远来看,它并没有这样做。(在我的本地系统中它完全正确)。我该如何解决这个问题?
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_())
跟进:
1 import sys
2 import datetime
3 import time
4 from PyQt4 import QtCore, QtGui
5
6 class Main(QtGui.QMainWindow):
7 def __init__(self, parent=None):
8 super(Main, self).__init__(parent)
9 self.b = QtGui.QPushButton("exit", self, clicked=self.close)
10
11 def showNowHour(self):
12 return datetime.datetime.now().strftime("%H")
13
14 def showNowMinute(self):
15 return datetime.datetime.now().strftime("%M")
16
17 def myRun(self):
18 while True:
19 time.sleep(2)
20 hour = int(self.showNowHour())
21 minute = int(self.showNowMinute())
22 print "[Debug]: " + str(hour) + " " + str(minute)
23 if (hour>8 and hour<15):
24 print "is open"
25 else:
26 print "is close"
27
28 if __name__ == "__main__":
29 app=QtGui.QApplication(sys.argv)
30 myapp=Main()
31 myapp.show()
32 thread = QtCore.QThread()
33 thread.run = lambda myapp=myapp: myapp.myRun()
34 thread.start()
35 app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))
36 sys.exit(app.exec_())
37 while thread.isAlive():
38 app.processEvents()
39