Я делаю часы, используя Tkinter и Python.
Чего я хочу добиться, так это запустить часы и автоматически проверить, был ли обновлен файл JSON (который содержит визуальные настройки часов). Если файл JSON был обновлен, часы обновятся в режиме реального времени.
Однако это не так. Что мне нужно сделать, это обновить файл JSON, закрыть программу часов, а затем снова открыть программу часов. Только в этом случае измененные настройки JSON будут иметь место.
clock.py
from tkinter import *
from datetime import datetime
from settings import *
# Updates the program and clock settings
def tick():
time_string = datetime.now().strftime(time_format)
date_string = datetime.now().strftime(date_format)
root.config(bg=background_color)
container.configure(bg=background_color)
current_time.configure(text=time_string,
font=(time_font, time_size, time_weight, time_slant),
fg=time_color,
bg=background_color)
current_date.configure(text=date_string,
font=(date_font, date_size, date_weight, date_slant),
fg=date_color,
bg=background_color)
current_time.after(1, tick)
# TKInterface
root = Tk()
root.title(window_title)
# Binds 'Esc' key to exit program
root.bind('<Escape>', exit)
# Runs program in full-screen
if full_screen:
root.attributes('-fullscreen', True)
root.config(cursor='none')
# Creates container to hold time and date
container = Frame(root)
current_time = Label(container)
current_date = Label(container)
container.pack(expand=True)
current_time.pack()
current_date.pack()
tick()
root.mainloop()
settings.py
import os
import json
with open('settings.json') as json_settings:
settings = json.load(json_settings)
# Window
full_screen = settings['window']['full_screen']
window_title = settings['window']['window_title']
# Background
background_color = settings['background']['color']
# Time
time_font = settings['time']['font']
time_size = settings['time']['size']
time_weight = settings['time']['weight']
time_slant = settings['time']['slant']
time_color = settings['time']['color']
time_format = settings['time']['format']
# Date
date_font = settings['date']['font']
date_size = settings['date']['size']
date_weight = settings['date']['weight']
date_slant = settings['date']['slant']
date_color = settings['date']['color']
date_format = settings['date']['format']
settings.json
{
"window": {
"full_screen": false,
"window_title" : "chronoberry"
},
"background": {
"color": "black"
},
"time": {
"font": "arial",
"size": 70,
"weight": "bold",
"slant": "roman",
"color": "white",
"format": "%-I:%M:%S %p"
},
"date": {
"font": "arial",
"size": 20,
"weight": "normal",
"slant": "roman",
"color": "white",
"format": "%A, %B %-d %Y"
}
}
Желаемый эффект:
Если я изменю цвет фона в своем файле JSON и сохраню его, мои часы смогут обновлять его цвет во время выполнения.
Перед изменением цвета фона

После изменения цвета фона

Что я пробовал:
- Повторный импорт модуля settings.py с помощью importlib.reload (), но модуль settings.py не является допустимым аргументом.
- Открываем settings.json, читаем из него, закрываем и снова открываем. Однако после закрытия файла его невозможно открыть снова.