I 'm facing problems trying to embed 'clock' functionality in my application.
If you run the following code as is, you 'll see a window. Fine by now.
Now, uncomment lines 12 and 13 and run it again.
What should we see: a window open and a clock showing time in the output window of our programming IDE.
What we see: a window semi-open, 'struggling' to open, never loads and never shows completely, but the clock is working fine in the output window of our programming IDE.
So, what should I do to make the window load properly (and the clock to continue to work normally)?
If you run the following code as is, you 'll see a window. Fine by now.
Now, uncomment lines 12 and 13 and run it again.
What should we see: a window open and a clock showing time in the output window of our programming IDE.
What we see: a window semi-open, 'struggling' to open, never loads and never shows completely, but the clock is working fine in the output window of our programming IDE.
So, what should I do to make the window load properly (and the clock to continue to work normally)?
import sys from PyQt4 import QtGui import threading import datetime class MyApp(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.show() #clock = Clock() #clock.run() ############### CLASS CLOCK ##################### def ring_ring(): sys.stdout.write('ring ring\n') sys.stdout.flush() class Clock: def __init__(self): self.alarm_time = None self._alarm_thread = None self.update_interval = 1 self.event = threading.Event() def run(self): while True: self.event.wait(self.update_interval) if self.event.isSet(): break now = datetime.datetime.now() if self._alarm_thread and self._alarm_thread.is_alive(): alarm_symbol = '+' else: alarm_symbol = ' ' sys.stdout.write("\r%02d:%02d:%02d %s" % (now.hour, now.minute, now.second, alarm_symbol)) sys.stdout.flush() def set_alarm(self, hour, minute): now = datetime.datetime.now() alarm = now.replace(hour=int(hour), minute=int(minute)) delta = int((alarm - now).total_seconds()) if delta <= 0: alarm = alarm.replace(day=alarm.day + 1) delta = int((alarm - now).total_seconds()) if self._alarm_thread: self._alarm_thread.cancel() self._alarm_thread = threading.Timer(delta, ring_ring) self._alarm_thread.daemon = True self._alarm_thread.start() if __name__ == '__main__': app = QtGui.QApplication(sys.argv) window = MyApp() window.show() sys.exit(app.exec_())