[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Make an LED-style clock in tkinter and Python

[An LED-style clock made with tkinter and Python]

My previous post Draw LED-style characters in tkinter and Python explains how to make an Led class that draws LED-style letters, numbers, and a few other characters. This post uses that class to build an LED-style clock. This is really fairly simple compared to the previous post; it's mostly a matter of arranging the Led polygons nicely.

The Clock

The following make_leds method arranges the Led objects.

def make_leds(self, wid, hgt): '''Draw time LEDs.''' self.leds = [] margin = 5 cell_width = 20 cell_height = 30 led_thickness = 4 gap = 1 on_color = 'lime' off_color = '#003000' x = y = margin self.leds.append(Led(self.canvas, (x, y), cell_width, cell_height, led_thickness, gap, on_color, off_color, '7')) x += cell_width + margin self.leds.append(Led(self.canvas, (x, y), cell_width, cell_height, led_thickness, gap, on_color, off_color, '6')) x += cell_width colon_space = 4 colon_width = 6 colon_spacing = (cell_height - 2 * colon_width) / 3 x += colon_space x1 = x x2 = x1 + colon_width y1 = y + colon_spacing y2 = y1 + colon_width self.canvas.create_rectangle(x1, y1, x2, y2, fill=on_color) y1 += colon_width + colon_spacing y2 = y1 + colon_width self.canvas.create_rectangle(x1, y1, x2, y2, fill=on_color) x += colon_width + colon_space self.leds.append(Led(self.canvas, (x, y), cell_width, cell_height, led_thickness, gap, on_color, off_color, '3')) x += cell_width + margin self.leds.append(Led(self.canvas, (x, y), cell_width, cell_height, led_thickness, gap, on_color, off_color, '5')) x += cell_width x += colon_space x1 = x x2 = x1 + colon_width y1 = y + colon_spacing y2 = y1 + colon_width self.canvas.create_rectangle(x1, y1, x2, y2, fill=on_color) y1 += colon_width + colon_spacing y2 = y1 + colon_width self.canvas.create_rectangle(x1, y1, x2, y2, fill=on_color) x += colon_width + colon_space self.leds.append(Led(self.canvas, (x, y), cell_width, cell_height, led_thickness, gap, on_color, off_color, '3')) x += cell_width + margin self.leds.append(Led(self.canvas, (x, y), cell_width, cell_height, led_thickness, gap, on_color, off_color, '5')) x += cell_width + margin - 1 y += cell_height + margin self.window.geometry(f'{x}x{y}')

This method defines some geometric parameters. It then creates some Led objects, saving them in the self.leds list so they're easy to find later. After it creates each object, it updates the x variable to position the next Led moved to the right.

After positioning the second and fourth Leds, the code draws a colon to separate hours from minutes and minutes from seconds.

None of this is very hard, it's just a matter of bookkeeping and arranging things nicely.

The method finishes by adding a little space to the right of and below the last Led and then setting the window's size to fit the clock.

Clock Ticks

After it finishes building the clock, the program calls the following tick method.

def tick(self): '''Update the clock.''' time = datetime.now().strftime('%H%M%S') for i in range(6): self.leds[i].set_letter(time[i]) # Tick again in 1 second. self.window.after(1000, self.tick)

This method gets the current time and converts it into a string holding the hours, minutes, and seconds with no separator as in 134924. It then loops through the digits in that string and assigns each to the corresponding Led object to make it display that digit. That's all the program needs to do to display the current time.

The method finishes by using after to schedule the tick method to run again in 1000 milliseconds or 1 second.

Window Decorations

To make the clock more useable, the program removes the windows title bar and borders, allows you to move the clock by clicking and dragging it, and makes the clock a topmost window. For more information on those tasks, see these posts:

Conclusion

The Led class makes building an analog clock relatively easy. The only tricky part is arranging the Led objects nicely. The program's other techniques (removing window decorations, moving via dragging, and making it topmost) are all pretty easy if you read the earlier posts.

Download the example to experiment with it and to see additional details.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.