Build a Simple Digital Clock with QLCDNumber in PyQt6

In this PyQt6 tutorial, I will show you how to make a simple digital clock in LCD Style.

It will be quite detailed, so that you will be able to follow along even if you are new to PyQt6. I am using the QLCDNumber widget to create the clock display and update it in real-time using a timer.

By the end of this tutorial, you will have a working digital clock application that you can customize or expand with additional features.

If you have not installed PyQt6 in your system, this blog will guide you on “How to Install PyQt6 on Different Platforms?“.

QLCDNumber in PyQt6

QLCDNumber is a special widget in PyQt6 that lets you display numbers in a style that looks like the digital numbers you see on LCDs.

The QLCDNumber widget displays numbers using that distinctive seven-segment style, where each digit is made up of lines that can be turned on or off to form different numbers.

It’s particularly useful when you want to create applications like timers, clocks, counters, or anything that needs to display changing numbers with that retro-digital aesthetic.

Read QSpinBox Widget in PyQt6

Create a Basic Window with QLCDNumber in PyQt6

Let’s start by creating a simple window using PyQt6. This is the foundation for any PyQt application:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow

# Create the application
app = QApplication(sys.argv)

# Create the main window
window = QMainWindow()
window.setWindowTitle("My First PyQt Window")
window.setGeometry(100, 100, 400, 200)  # x, y, width, height

# Show the window
window.show()

# Start the event loop
sys.exit(app.exec())

You can see what this code is going to give as output in the screenshot below.

Build a Simple Digital Clock with QLCDNumber in PyQt6

This code creates a basic, empty window. Let me explain what each part does:

  • QApplication is required for any PyQt app – it manages the application’s control flow
  • QMainWindow is a pre-made window class that gives us features like toolbars, status bars, etc.
  • setWindowTitle() sets the text in the window’s title bar
  • setGeometry() positions the window on the screen and sets its size
  • show() makes the window visible
  • app.exec() starts the application’s event loop, which keeps the window open

Check out How to Use QSlider Widget in PyQt6

Add a Layout

Now, let’s add a layout. Layouts in PyQt automatically arrange widgets for you, which is much better than positioning everything manually:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout

app = QApplication(sys.argv)

# Create the main window
window = QMainWindow()
window.setWindowTitle("Window with Layout")
window.setGeometry(100, 100, 400, 200)

# Create a central widget
central_widget = QWidget()
window.setCentralWidget(central_widget)

# Create a layout
layout = QVBoxLayout()
central_widget.setLayout(layout)

window.show()
sys.exit(app.exec())

You can see what this code is going to give as output in the screenshot below.

PyQt6 Build a Simple Digital Clock with QLCDNumber

The important new parts here:

  • We create a QWidget to act as the central widget of our main window
  • We create a QVBoxLayout (vertical box layout) that will arrange widgets from top to bottom
  • We set this layout on our central widget using setLayout()

The most important thing to understand is that QMainWindow requires a central widget to which we apply our layout. We can’t apply a layout directly to the main window itself.

Read How to Use QSlider Widget in PyQt6

Place a QLCDNumber Widget in the Layout

Now, let’s add a QLCDNumber widget to display digits in that cool LCD style:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLCDNumber

app = QApplication(sys.argv)

# Create the main window
window = QMainWindow()
window.setWindowTitle("Digital Display")
window.setGeometry(100, 100, 400, 200)

# Create a central widget
central_widget = QWidget()
window.setCentralWidget(central_widget)

# Create a layout
layout = QVBoxLayout()
central_widget.setLayout(layout)

# Create an LCD Number display
lcd = QLCDNumber()
lcd.setDigitCount(8)  # Display 8 digits
lcd.display("12:34:56")  # Show some initial value
lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled)  # Use filled segment style

# Add LCD to the layout
layout.addWidget(lcd)

window.show()
sys.exit(app.exec())

You can see what this code is going to give as output in the screenshot below.

How to Build a Simple Digital Clock with QLCDNumber in PyQt6

Let us see what is happening with our QLCDNumber widget:

  1. We create it with lcd = QLCDNumber()
  2. setDigitCount(8) specifies we want to show 8 digits (perfect for a time format like HH: MM)
  3. display("12:34:56") sets the initial value to display
  4. setSegmentStyle() changes how the digits look (Filled makes them look like a traditional LCD)
  5. Finally, we add the LCD to our layout with layout.addWidget(lcd)

Check out Handle Button Click Events Using Signals and Slots in PyQt6

A Real Clock

Let’s upgrade our example to make a functional digital clock that updates in real-time:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLCDNumber
from PyQt6.QtCore import QTimer, QTime

class DigitalClock(QMainWindow):
    def __init__(self):
        super().__init__()
        
        # Set up the window
        self.setWindowTitle("Digital Clock")
        self.setGeometry(100, 100, 400, 200)
        
        # Create central widget
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # Create layout
        layout = QVBoxLayout()
        central_widget.setLayout(layout)
        
        # Create LCD Number display
        self.lcd = QLCDNumber()
        self.lcd.setDigitCount(8)  # For HH:MM:SS format
        self.lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled)
        
        # Add LCD to layout
        layout.addWidget(self.lcd)
        
        # Set up timer to update the clock
        timer = QTimer(self)
        timer.timeout.connect(self.update_time)
        timer.start(1000)  # Update every second
        
        # Initialize with current time
        self.update_time()
    
    def update_time(self):
        current_time = QTime.currentTime()
        time_text = current_time.toString('hh:mm:ss')
        self.lcd.display(time_text)

# Create the application
app = QApplication(sys.argv)
clock = DigitalClock()
clock.show()
sys.exit(app.exec())

You can see what this code is going to give as output in the screenshot below.

Display the System Clock with QLCDNumber in PyQt6

Here’s what’s new:

  1. We’ve organized our code into a class for better structure
  2. We added a timer (QTimer) that triggers every 1000 milliseconds (1 second)
  3. When the timer triggers, it calls our update_time() method
  4. This method gets the current time and formats it as HH: MM
  5. We then update our LCD with the new time

The result is a clean, simple digital clock with that classic LCD look!

Read Arrange Widgets Using QGridLayout in PyQt6

Customize the Clock Display

Now that we have our basic digital clock working, let’s make it more attractive and useful by customizing its appearance. I’ll show you several simple ways to transform the plain QLCDNumber into something more visually appealing and functional.

Change QLCDNumber Appearance: Colors

You can handle how the digits appear using styles and background colors:

  • Segment Style: You’ve already used Filled But you can also use Outline or Flat.
  • Background/Foreground Color: Use stylesheets to change the color of the widget.
self.lcd.setStyleSheet("background-color: black; color: lime; border: 2px solid green;")
self.lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Outline)

This gives you a classic terminal look with green digits on a black background.

Add AM/PM or Date

If you want a 12-hour format with AM/PM:

time_text = QTime.currentTime().toString("hh:mm:ss AP")

To add the current date:

from PyQt6.QtWidgets import QLabel
self.date_label = QLabel()
self.date_label.setText(QDate.currentDate().toString("dd MMM yyyy"))
layout.addWidget(self.date_label)

Make It More Visually Appealing

We can add padding, font styles, and more widgets to polish the layout:

self.lcd.setStyleSheet("""
    background-color: navy;
    color: cyan;
    border-radius: 10px;
    font-size: 30px;
""")

Check out How to Use QVBoxLayout in PyQt6 for Vertical Layouts?

Stylish Digital Clock with AM/PM, Date, and Custom Styling

Let us combine all the properties that we learn above and see the final result.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLCDNumber, QLabel
from PyQt6.QtCore import QTimer, QTime, QDate
from PyQt6.QtGui import QPalette, QColor

class StylishDigitalClock(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Stylish Digital Clock")
        self.setGeometry(100, 100, 500, 300)
        
        # Set background color
        self.setStyleSheet("background-color: #2c3e50;")

        # Central widget and layout
        central_widget = QWidget()
        layout = QVBoxLayout()
        layout.setContentsMargins(20, 20, 20, 20)  # Add padding
        layout.setSpacing(15)
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

        # Date label
        self.date_label = QLabel()
        self.date_label.setStyleSheet("""
            color: #ecf0f1;
            font-size: 18px;
            font-weight: bold;
        """)
        layout.addWidget(self.date_label)

        # LCD Clock
        self.lcd = QLCDNumber()
        self.lcd.setDigitCount(11)  # Enough for hh:mm:ss AM/PM
        self.lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled)
        self.lcd.setStyleSheet("""
            QLCDNumber {
                background-color: #1abc9c;
                border-radius: 20px;
                padding: 10px;
                color: #2c3e50;
            }
        """)
        layout.addWidget(self.lcd)

        # Timer for updates
        timer = QTimer(self)
        timer.timeout.connect(self.update_display)
        timer.start(1000)  # 1 second

        # Initial update
        self.update_display()

    def update_display(self):
        # Time in AM/PM format
        time = QTime.currentTime()
        time_str = time.toString("hh:mm:ss AP")
        self.lcd.display(time_str)

        # Current date
        date = QDate.currentDate()
        self.date_label.setText(date.toString("dddd, MMMM d, yyyy"))

# Run the app
if __name__ == "__main__":
    app = QApplication(sys.argv)
    clock = StylishDigitalClock()
    clock.show()
    sys.exit(app.exec())

You can see the output in the screenshot below.

Create a Digital Clock in PyQt6 Using QLCDNumber
  • Custom segment style using Filled and a styled background.
  • Displays AM/PM time format.
  • Shows the current date at the top.
  • Uses rounded corners, padding, and background color for visual appeal.

In this article, I helped you learn how to build a simple digital clock with QLCDNumber in PyQt6. We started by creating a basic window and added layouts, and I also explained how to add customization to the clock display and I have shown you the final output.

You may like to learn all about PyQt6:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.