Modern Clock with PyQt
Below is an example of a modern clock created using PyQt. This clock includes a digital display, analog display, and a countdown timer. The countdown timer is a new feature that allows users to set a timer for a specific amount of time.
python Copy code
import sys
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout, QPushButton, QTimeEdit, QSpinBox
class Clock(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 400, 300)
self.setWindowTitle('Modern Clock')
# Create layout
main_layout = QVBoxLayout()
self.setLayout(main_layout)
# Create digital clock
self.digital_clock = QLabel()
self.digital_clock.setFont(QFont('Arial', 40))
self.digital_clock.setAlignment(Qt.AlignCenter)
main_layout.addWidget(self.digital_clock)
# Create analog clock
self.analog_clock = QLabel()
self.analog_clock.setFont(QFont('Arial', 40))
self.analog_clock.setAlignment(Qt.AlignCenter)
main_layout.addWidget(self.analog_clock)
# Create countdown timer
countdown_layout = QHBoxLayout()
main_layout.addLayout(countdown_layout)
self.countdown_time = QTimeEdit()
self.countdown_time.setDisplayFormat('HH:mm:ss')
countdown_layout.addWidget(self.countdown_time)
self.countdown_button = QPushButton('Start Countdown')
self.countdown_button.clicked.connect(self.start_countdown)
countdown_layout.addWidget(self.countdown_button)
self.countdown_label = QLabel()
self.countdown_label.setFont(QFont('Arial', 20))
countdown_layout.addWidget(self.countdown_label)
# Create timer
self.timer = QTimer()
self.timer.timeout.connect(self.update_time)
self.timer.start(1000)
# Create countdown timer
self.countdown_timer = QTimer()
self.countdown_timer.timeout.connect(self.update_countdown)
def update_time(self):
# Update digital clock
from datetime import datetime
now = datetime.now()
self.digital_clock.setText(now.strftime('%H:%M:%S'))
# Update analog clock
hours = now.hour % 12
minutes = now.minute
seconds = now.second
self.analog_clock.setText(f'{hours:02d}:{minutes:02d}:{seconds:02d}')
def start_countdown(self):
# Get countdown time
countdown_time = self.countdown_time.time()
hours = countdown_time.hour()
minutes = countdown_time.minute()
seconds = countdown_time.second()
# Calculate total seconds
total_seconds = hours * 3600 + minutes * 60 + seconds
# Start countdown timer
self.countdown_timer.start(1000)
self.countdown_seconds = total_seconds
def update_countdown(self):
# Update countdown label
hours = self.countdown_seconds // 3600
minutes = (self.countdown_seconds % 3600) // 60
seconds = self.countdown_seconds % 60
self.countdown_label.setText(f'{hours:02d}:{minutes:02d}:{seconds:02d}')
# Decrement countdown seconds
self.countdown_seconds -= 1
# Stop countdown timer when finished
if self.countdown_seconds < 0:
self.countdown_timer.stop()
self.countdown_label.setText('Countdown finished!')
def main():
app = QApplication(sys.argv)
clock = Clock()
clock.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Example Use Cases:
Run the code to display the modern clock.
Click on the "Start Countdown" button to start a countdown timer.
Set the countdown time using the QTimeEdit widget.
The countdown timer will decrement every second until it reaches zero.
Note: This code uses PyQt5, which is a set of Python bindings for the Qt application framework. Make sure you have PyQt5 installed in your Python environment to run this code.