Skip to main content
added 3917 characters in body
Source Link
Mike - SMT
  • 526
  • 3
  • 16
  1. delete from tkinter import * this is for 2 reason. The first is you are already doing import tkinter as tk so you do not need a 2nd import of tkinter. Two import * can cause problems if imported names overlap and get overwritten.

  2. Remove imports you are not using. IE: calenddar, sys

  3. Fix clean up formatting to follow PEP8. Single spaces between methods double spaces between functions/class and so on.

  4. Remove unneeded/redundant comments. Comments should be reserver for explaining things that are not obvious in the code.

  5. You write you tick code twice. One in __init__ and once in tick. You can remove the instance in __init__ and simply call tick right away while adding a default value for self.time and self.display_time in the init.

  6. 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.

  7. The class FullScreenApp is never used. So either implement it somewhere or remove it.

  8. Your return in phrase_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.

  9. Your quit button does not need a lambda. You can save a reference to quit.

  10. 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

  11. Lastly youYou are already inheriting from tk.Label so why not do the same for the root window?

  12. Lastly you do not need spacer labels. You can use padx and pady 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()
  1. delete from tkinter import * this is for 2 reason. The first is you are already doing import tkinter as tk so you do not need a 2nd import of tkinter. Two import * can cause problems if imported names overlap and get overwritten.

  2. Remove imports you are not using. IE: calenddar, sys

  3. Fix clean up formatting to follow PEP8. Single spaces between methods double spaces between functions/class and so on.

  4. Remove unneeded/redundant comments.

  5. You write you tick code twice. One in __init__ and once in tick. You can remove the instance in __init__ and simply call tick right away while adding a default value for self.time and self.display_time in the init.

  6. 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.

  7. The class FullScreenApp is never used. So either implement it somewhere or remove it.

  8. Your return in phrase_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.

  9. Your quit button does not need a lambda. You can save a reference to quit.

  10. 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

  11. Lastly you are already inheriting from tk.Label so why not do the same for the root window?

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 = tk.Label(self, text="", fg="white", bg="black")
        s = tk.Label(self, text="", fg="white", bg="black")
        p = tk.Label(self, text="", fg="white", bg="black")
        v.pack(anchor='nw')
        clock1.pack(anchor='nw')
        w.pack(anchor='nw')
        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")
        v.grid(row=1, column=0)
        clock1.grid(row=2, column=0)
        w.grid(row=3, column=0)
        s.grid(row=4, column=0)
        p.grid(row=5, column=0)

        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()
  1. delete from tkinter import * this is for 2 reason. The first is you are already doing import tkinter as tk so you do not need a 2nd import of tkinter. Two import * can cause problems if imported names overlap and get overwritten.

  2. Remove imports you are not using. IE: calenddar, sys

  3. Fix clean up formatting to follow PEP8. Single spaces between methods double spaces between functions/class and so on.

  4. Remove unneeded/redundant comments. Comments should be reserver for explaining things that are not obvious in the code.

  5. You write you tick code twice. One in __init__ and once in tick. You can remove the instance in __init__ and simply call tick right away while adding a default value for self.time and self.display_time in the init.

  6. 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.

  7. The class FullScreenApp is never used. So either implement it somewhere or remove it.

  8. Your return in phrase_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.

  9. Your quit button does not need a lambda. You can save a reference to quit.

  10. 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

  11. You are already inheriting from tk.Label so why not do the same for the root window?

  12. Lastly you do not need spacer labels. You can use padx and pady 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)
        clock1.pack(anchor='nw', pady=(25, 0))
        w.pack(anchor='nw', pady=(0, 30))
        
        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)
        clock1.grid(row=2, column=0, pady=(25, 0))
        w.grid(row=3, column=0, pady=(0, 30))

        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()
added 3917 characters in body
Source Link
Mike - SMT
  • 526
  • 3
  • 16

Lastly I am not sure if you wanted to have the timer at the top left. Judging by the fact you fullscreen I am guessing you want it centered. For this I would prefer to use grid() over pack() as grid allows us to set weights for rows and columns making it IMO easier to center everything.

See this example:

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")
        v.grid(row=1, column=0)
        clock1.grid(row=2, column=0)
        w.grid(row=3, column=0)
        s.grid(row=4, column=0)
        p.grid(row=5, column=0)

        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()

Lastly I am not sure if you wanted to have the timer at the top left. Judging by the fact you fullscreen I am guessing you want it centered. For this I would prefer to use grid() over pack() as grid allows us to set weights for rows and columns making it IMO easier to center everything.

See this example:

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")
        v.grid(row=1, column=0)
        clock1.grid(row=2, column=0)
        w.grid(row=3, column=0)
        s.grid(row=4, column=0)
        p.grid(row=5, column=0)

        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()
Source Link
Mike - SMT
  • 526
  • 3
  • 16

Good start but a few things should be cleaned up.

Try to follow a standard. Preferably PEP8. You have extra spacing between = and no spacing between , in places and its messy.

  1. delete from tkinter import * this is for 2 reason. The first is you are already doing import tkinter as tk so you do not need a 2nd import of tkinter. Two import * can cause problems if imported names overlap and get overwritten.

  2. Remove imports you are not using. IE: calenddar, sys

  3. Fix clean up formatting to follow PEP8. Single spaces between methods double spaces between functions/class and so on.

  4. Remove unneeded/redundant comments.

  5. You write you tick code twice. One in __init__ and once in tick. You can remove the instance in __init__ and simply call tick right away while adding a default value for self.time and self.display_time in the init.

  6. 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.

  7. The class FullScreenApp is never used. So either implement it somewhere or remove it.

  8. Your return in phrase_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.

  9. Your quit button does not need a lambda. You can save a reference to quit.

  10. 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

  11. Lastly you are already inheriting from tk.Label so why not do the same for the root window?

See below reworked example. Let me know if you have any questions.

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 = tk.Label(self, text="", fg="white", bg="black")
        s = tk.Label(self, text="", fg="white", bg="black")
        p = tk.Label(self, text="", fg="white", bg="black")
        v.pack(anchor='nw')
        clock1.pack(anchor='nw')
        w.pack(anchor='nw')
        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()