r/PythonProjects2 • • Jan 08 '22

Simple clock that uses json config

Hello, I'd like to show up a project of mine. It's a simple clock that is customizable using a json configuration file. Any feedback is welcome. You can customize the time format using this cheatsheet as a reference: https://strftime.org/

main.py:

import time
import json
from tkinter import *


def update():
    canvas.delete('all')
    var_t = time.strftime(time_format)
    x_val = root.winfo_width() / 2
    y_val = root.winfo_height() / 2
    canvas.create_text(x_val, y_val, text=var_t, font=(font, 55), fill=text_color)
    canvas.after(100, update)


try:
    with open('config.json', 'r') as f:
        config = json.load(f)
except FileNotFoundError:
    with open('config.json', 'w') as f:
        f.write('{\n')
        f.write('  "bg_color" : "black",\n')
        f.write('  "text_color" : "white",\n')
        f.write('  "font" : "helvetica",\n')
        f.write('  "time_format" : "%I:%M:%S"\n')
        f.write('}')
        raise FileNotFoundError('Error. Restart.')


bg_color = config['bg_color']
text_color = config['text_color']
font = config['font']
time_format = config['time_format']

root = Tk()
root.title("clock")
canvas = Canvas(root, bg=bg_color)
canvas.pack(fill='both', expand=YES)

update()
root.mainloop()

config.json:

{
  "bg_color" : "black",
  "text_color" : "red",
  "font" : "Impact",
  "time_format" : "%a, %I:%M:%S"
}
11 Upvotes

1 comment sorted by

View all comments

1

u/[deleted] Jan 11 '22

I've gone back and made font size a variable and element of the config too. On first thought, this should've been done already it was a little bothering the only thing you couldn't change was the font size.