Dynamic real-time clock display in Python
Solved
Hello.
I'm using the following code to display the current time in a
My question is how to display the date every second, i.e., create a dynamic clock?
I'm using the following code to display the current time in a
Labelnamed
t1with the
PyQt5module 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
Hello,
A quick Google search will take you, for example, to this page, which exactly addresses your problem.
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:
Good luck!
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 throughconnect
to the methodshowTime
; - the method
showTime
updates the display; - the main program only creates and launches the application.
Good luck!
yg_be
Posted messages
23437
Registration date
Status
Contributor
Last intervention
Ambassadeur
1 588
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).
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).
Thank you for the response. Here is my code:
I want to know how to create a loop with a pause.
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.