I have designed a basic interface for a smart mirror I made. I coded it to include the time, date, and several phrases that refresh every 24 hours. The phrases are just some random quotes as examples.
I was wondering if there were anyways I could streamline this code or even improve the layout of the interface.
import tkinter as tk
import sys
import time
import calendar
import random
import datetime as dt
from tkinter import *
# Root is the name of the Tkinter Window. This is important to remember.
root=tk.Tk()
""" DICTIONARY PHRASES """
phrases = [" I never had a policy; I have just tried to do my very best each and every day. -- Abraham Lincoln",
" There are some things you learn best in calm, and some in storm. -- Willa Cather",
" If a man does his best, what else is there? -- George S. Patton"]
class Clock(tk.Label):
""" Class that contains the clock widget and clock refresh """
def __init__(self, parent=None, seconds=True, colon=False):
"""
Create and place the clock widget into the parent element
It's an ordinary Label element with two additional features.
"""
tk.Label.__init__(self, parent)
self.display_seconds = seconds
if self.display_seconds:
self.time = time.strftime('%I:%M:%S %p')
else:
self.time = time.strftime('%I:%M:%S %p').lstrip('0')
self.display_time = self.time
self.configure(text=self.display_time, width=11)
if colon:
self.blink_colon()
self.after(200, self.tick)
def tick(self):
""" Updates the display clock every 200 milliseconds """
if self.display_seconds:
new_time = time.strftime('%I:%M:%S %p')
else:
new_time = time.strftime('%I:%M:%S %p').lstrip('0')
if new_time != self.time:
self.time = new_time
self.display_time = self.time
self.config(text=self.display_time)
self.after(200, self.tick)
def blink_colon(self):
""" Blink the colon every second """
if ':' in self.display_time:
self.display_time = self.display_time.replace(':',' ')
else:
self.display_time = self.display_time.replace(' ',':',1)
self.config(text=self.display_time)
self.after(1000, self.blink_colon)
class FullScreenApp(object):
def __init__(self, master, **kwargs):
self.master=master
pad=3
self._geom='200x200+0+0'
master.geometry("{0}x{1}+0+0".format(
master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
master.bind('<Escape>',self.toggle_geom)
def toggle_geom(self,event):
geom=self.master.winfo_geometry()
print(geom,self._geom)
self.master.geometry(self._geom)
self._geom=geom
def phrase_refresh():
new_phrase = random.choice(phrases)
e.configure(text=new_phrase, wrap=650) # e is your label
root.after(86400, phrase_refresh) # Delay measured in milliseconds. Currently set to 24 hours. (There are 86400 milliseconds in a day)
return new_phrase
# Sets background color to black
root.configure(bg="black")
# Removes the window bar at the top creating a truely fullscreen
root.wm_attributes('-fullscreen','true')
tk.Button(root, text="", bg="black", fg="black", command=lambda root=root:quit(root)).pack()
#Spacer
v = Label(root, text="", fg="white", bg="black")
v.pack(anchor=NW)
# this displays the clock known as clock1
clock1 = Clock(root)
clock1.pack(anchor=NW)
# This gives the clock format.
clock1.configure(bg='black',fg='white',font=("helvetica",60))
# Add the date to the tkinter window
w = Label(root, text=f"{dt.datetime.now():%a, %b %d %Y}", fg="white", bg="black", font=("helvetica", 30), width=16)
w.pack(anchor=NW)
#Spacer
s = Label(root, text="", fg="white", bg="black")
s.pack(anchor=NW)
#Spacer
p = Label(root, text="", fg="white", bg="black")
p.pack(anchor=NW)
# Add the phrase to the tkinter window
e = Label(root, fg="white", bg="black", font=("helvetica", 17))
phrase_refresh()
e.pack(anchor=NW)
root.mainloop()