Python Forum
Problem with 'Clock' functionality
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with 'Clock' functionality
#1
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)?


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_())
Reply
#2
Maybe I should use something like:
fpsClock = pygame.time.Clock()
which is used in games?
Reply
#3
There are own function for this in PyQt like QDate, QTime, and QDateTime.
A lcd clock example using QTime.
import sys from PyQt4 import QtGui, QtCore class Example(QtGui.QWidget):     def __init__(self):         super().__init__()         self.clock()         timer = QtCore.QTimer(self)         timer.timeout.connect(self.showlcd)         timer.start(1000)         self.showlcd()     def clock(self):         self.lcd = QtGui.QLCDNumber(self)         self.lcd.setDigitCount(8)         self.setGeometry(30, 30, 300, 150)         self.setWindowTitle('Time')         vbox = QtGui.QVBoxLayout()         vbox.addWidget(self.lcd)         self.setLayout(vbox)         self.show()     def showlcd(self):         time = QtCore.QTime.currentTime()         text = time.toString('hh:mm:ss')         self.lcd.display(text) if __name__ == '__main__':     app = QtGui.QApplication(sys.argv)     ex = Example()     sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Monitor Computer Internal Clock EdMcCauley 3 1,219 May-10-2025, 01:52 AM
Last Post: deanhystad
  Alarm Clock [tkinter] moon 5 3,044 Aug-07-2024, 05:44 PM
Last Post: deanhystad
  World Clock syntax error OscarBoots 1 1,730 May-03-2024, 05:20 AM
Last Post: snippsat
  Priority Queue with update functionality PythonNewbee 4 6,814 Dec-29-2022, 12:13 PM
Last Post: Yoriz
  Module 'time' has no attribute 'clock' Sophie 4 7,049 Jan-25-2022, 08:05 PM
Last Post: Sophie
  Clock\time calculation script Drone4four 3 3,916 Jan-21-2022, 03:44 PM
Last Post: ibreeden
  time.clock not functioning Oldman45 3 4,586 Apr-07-2021, 08:51 AM
Last Post: Oldman45
  Validating the functionality of the application rpalakodety 1 2,781 Dec-30-2019, 07:58 PM
Last Post: ndc85430
  Cannot use function with timer/clock theangryprogrammer 1 4,583 Jan-22-2019, 04:22 PM
Last Post: Larz60+
  I'm having trouble with an OOP version of Pickle functionality CodeWolf 2 3,837 Dec-19-2018, 05:41 PM
Last Post: CodeWolf

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.