Dynamic real-time clock display in Python

Solved
Hello.

I'm using the following code to display the current time in a
Label
named
t1
with the
PyQt5
module of python.
My question is how to display the date every second, i.e., create a dynamic clock?

now = datetime.now() t2 = now.strftime("%H:%M:%S") win.t1.setText(t2)

4 answers

  1. Moderator
    Hello,

    A quick Google search will take you, for example, to this page, which exactly addresses your problem.

    # importing required library import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtWidgets import QVBoxLayout, QLabel from PyQt5.QtGui import QFont from PyQt5.QtCore import QTimer, QTime, Qt class Window(QWidget): def __init__(self): super().__init__() # setting geometry of main window self.setGeometry(100, 100, 800, 400) # creating a vertical layout layout = QVBoxLayout() # creating font object font = QFont('Arial', 120, QFont.Bold) # creating a label object self.label = QLabel() # setting centre alignment to the label self.label.setAlignment(Qt.AlignCenter) # setting font to the label self.label.setFont(font) # adding label to the layout layout.addWidget(self.label) # setting the layout to main window self.setLayout(layout) # creating a timer object timer = QTimer(self) # adding action to timer timer.timeout.connect(self.showTime) # update the timer every second timer.start(1000) # method called by timer def showTime(self): # getting current time current_time = QTime.currentTime() # converting QTime object to string label_time = current_time.toString('hh:mm:ss') # showing it to the label self.label.setText(label_time) # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # showing all the widgets window.show() # start the app App.exit(App.exec_())


    Contrary to what was said earlier, using a loop is not the right approach, as all modules for displaying a graphical interface (whether it’s PyQt, GTK, or Tk) use a main loop to run the program. In terms of implementation, this means that your entire program reacts to events (for example, a mouse click, keyboard input, or, in this case, a time step), and the main program only launches the window.

    Now, let's break down the program above:
    • timer
      triggers an event every second; this event is triggered through
      connect
      to the method
      showTime
      ;
    • the method
      showTime
      updates the display;
    • the main program only creates and launches the application.


    Good luck!
    1
    1. Contributor
      Hello,

      you are only showing part of your code.

      Please use code tags when sharing code: https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code

      I suggest making a loop, in which you also include a pause of one second (or less, depending on the precision required).
      0
      1. Thank you for the response. Here is my code:
        from datetime import datetime from PyQt5.uic import loadUi from PyQt5.QtWidgets import QApplication app=QApplication([]) win=loadUi('arith.ui') win.show() now = datetime.now() t2 = now.strftime("%H:%M:%S") win.t1.setText(t2) app.exec_()


        I want to know how to create a loop with a pause.
        0
      2. Contributor
        @HAJRI64An example of a loop with a pause:
        import time while True: time.sleep(1) print("hello")
        0
    2. I tried that, but it didn't work with the PyQt5 graphical interface.
      0
      1. Contributor
        How did you do it?
        0
      2. Contributor
        Can you share the UI file? It would allow me to test it.
        0
    3. from datetime import datetime import time from PyQt5.uic import loadUi from PyQt5.QtWidgets import QApplication app=QApplication([]) win=loadUi('time.ui') win.show() now = datetime.now() t2 = now.strftime("%H:%M:%S") win.t1.setText(t2)


      Note:
      t1
      is the time display area. it is a
      Label
      area.
      0
      1. Contributor
        I don't see a loop in the shared code.

        Can you use the code tags, as explained in #1?

        Can you share the time.ui file? This would allow me to test it.
        0