This is the same script i used in my local system where it work exactly. The same script is using in another location where its also a same system same setup. But not working.
I can not find anything wrong myself on this. Any idea if its a Python Bug?
What it does runs for 24/7 and shows a simple Gui before 9AM and after 3PM (PC has 24 hour time mode not AM or PM) . But its not doing in long run. (in my local system its doing exactly). How do i resolve this?
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_())
Follow up:
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
This possibly fails because you are calling GUI methods from a different thread. You should always perform GUI calls within the main thread (aka “GUI thread”), otherwise it is easy to run into problems.
Instead of a thread which sleeps for two seconds on every loop, replace it with a
QTimerthat fires every 2 seconds. Put something like this in your mainline:In your
Mainclass replace themyRunmethod with:Isn’t it nice not to need threads!