delete
from tkinter import *
this is for 2 reason. The first is you are already doingimport tkinter as tk
so you do not need a 2nd import of tkinter. Twoimport *
can cause problems if imported names overlap and get overwritten.Remove imports you are not using. IE: calenddar, sys
Fix clean up formatting to follow PEP8. Single spaces between methods double spaces between functions/class and so on.
Remove unneeded/redundant comments. Comments should be reserver for explaining things that are not obvious in the code.
You write you
tick
code twice. One in__init__
and once intick
. You can remove the instance in__init__
and simply call tick right away while adding a default value forself.time
andself.display_time
in the init.You use
200
milliseconds in your after statement. This is 5 times more than you need. 1 second intervals will suffice. There is no need to call the function 5 times more than is needed.The class
FullScreenApp
is never used. So either implement it somewhere or remove it.Your
return
inphrase_refresher
does nothing for you. Its returning to a call with no variable assignment and then its returning to itself forever after that. You can remove the return.Your quit button does not need a lambda. You can save a reference to quit.
Your quit button has no word and is the same color as background. This will make it very had for someone to click it :D
Lastly youYou are already inheriting from
tk.Label
so why not do the same for the root window?Lastly you do not need spacer labels. You can use
padx
andpady
to determine spacing between widgets.
import tkinter as tk
import datetime as dt
import random
import time
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, width=11)
self.display_seconds = seconds
self.time = None
if colon:
self.blink_colon()
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')
print(new_time)
if self.time is not None:
if new_time != self.time:
self.time = new_time
self.display_time = self.time
self.config(text=self.display_time)
else:
self.time = new_time
self.after(1000, 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 App(tk.Tk):
def __init__(self):
super().__init__()
self.configure(bg="black")
self.wm_attributes('-fullscreen', 'true')
tk.Button(self, text="Quit", bg="black", fg="white", command=self.quit).pack()
clock1 = Clock(self)
clock1.configure(bg='black', fg='white', font=("helvetica", 60))
w = tk.Label(self, text=f"{dt.datetime.now():%a, %b %d %Y}",
fg="white", bg="black", font=("helvetica", 30), width=16)
v = tkclock1.Labelpack(self, text="", fg="white"anchor='nw', bg="black")
s = tk.Labelpady=(self, text="", fg="white"25, bg="black"0)
p = tk.Label(self, text="", fg="white", bg="black")
vw.pack(anchor='nw')
, clock1.packpady=(anchor='nw')
0, w.pack(anchor='nw'30)
s.pack(anchor='nw')
p.pack(anchor='nw')
self.e = tk.Label(self, fg="white", bg="black", font=("helvetica", 17))
self.phrase_refresh()
self.e.pack(anchor='nw')
def phrase_refresh(self):
new_phrase = random.choice(phrases)
self.e.configure(text=new_phrase, wrap=650)
self.after(86400, self.phrase_refresh)
App().mainloop()
import tkinter as tk
import datetime as dt
import random
import time
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, width=11)
self.display_seconds = seconds
self.time = None
if colon:
self.blink_colon()
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')
print(new_time)
if self.time is not None:
if new_time != self.time:
self.time = new_time
self.display_time = self.time
self.config(text=self.display_time)
else:
self.time = new_time
self.after(1000, 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 App(tk.Tk):
def __init__(self):
super().__init__()
self.configure(bg="black")
self.wm_attributes('-fullscreen', 'true')
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
tk.Button(self, text="Quit", bg="black", fg="white", command=self.quit).grid(row=0, column=0)
frame = tk.Frame(self, bg="black")
frame.grid(row=1, column=0)
clock1 = Clock(frame)
clock1.configure(bg='black', fg='white', font=("helvetica", 60))
w = tk.Label(frame, text=f"{dt.datetime.now():%a, %b %d %Y}",
fg="white", bg="black", font=("helvetica", 30), width=16)
v = tk.Label(frame, text="", fg="white", bg="black")
s = tk.Label(frame, text="", fg="white", bg="black")
p = tk.Label(frame, text="", fg="white", bg="black")
vclock1.grid(row=1row=2, column=0)
, clock1.gridpady=(row=225, column=00))
w.grid(row=3, column=0)
s.grid(row=4, column=0)
p.gridpady=(row=50, column=030))
self.e = tk.Label(frame, fg="white", bg="black", font=("helvetica", 17))
self.phrase_refresh()
self.e.grid(row=6, column=0)
def phrase_refresh(self):
new_phrase = random.choice(phrases)
self.e.configure(text=new_phrase, wrap=650)
self.after(86400, self.phrase_refresh)
App().mainloop()