Skip to main content
added 130 characters in body
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Also a class is overkill. Finally, your use of %H is likely incorrect given that you also include %p; you probably want %I for a 12-hour clock.

This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%I:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%I:%M %p'))

        until_next = round(
            1000 * (60 - time()%60)
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Also a class is overkill. Finally, your use of %H is likely incorrect given that you also include %p; you probably want %I for a 12-hour clock.

This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%I:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%I:%M %p'))

        until_next = round(
            1000 * (60 - time()%60)
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Finally, your use of %H is likely incorrect given that you also include %p; you probably want %I for a 12-hour clock.

This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%I:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%I:%M %p'))

        until_next = round(
            1000 * (60 - time()%60)
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()
added 130 characters in body
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Also a class is overkill. Finally, your use of %H is likely incorrect given that you also include %p; you probably want %I for a 12-hour clock.

This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%H'%I:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%H'%I:%M %p'))

        until_next = round(
            1000 * (60 - time()%60)
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Also a class is overkill. This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%H:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%H:%M %p'))

        until_next = round(
            1000 * (60 - time()%60)
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Also a class is overkill. Finally, your use of %H is likely incorrect given that you also include %p; you probably want %I for a 12-hour clock.

This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%I:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%I:%M %p'))

        until_next = round(
            1000 * (60 - time()%60)
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()
No need for fmod
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Also a class is overkill. This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%H:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from math import fmod
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%H:%M %p'))

        until_next = round(
            1000 * (60 - fmod(time(), 60)%60)
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Also a class is overkill. This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%H:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from math import fmod
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%H:%M %p'))

        until_next = round(
            1000 * (60 - fmod(time(), 60))
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()

To start, it's crucial that you stop creating a brand new label ten times a second. Just modify the existing one. Also, this is so simple that a class is not called for. Move as much as possible away from your thread, into your setup routine. Also a class is overkill. This all suggests:

from datetime import datetime
import tkinter as tk
from threading import Thread
from time import sleep


def main():
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def get():
        while True:
            now = datetime.now()
            lbl.config(text=now.strftime('%H:%M %p'))
            sleep(0.1)

    receive_thread = Thread(target=get)
    receive_thread.start()
    display.mainloop()


if __name__ == '__main__':
    main()

Ten times a second is overkill, and you can safely make this much sleepier. Do not make a thread at all; use an after() timer, and calculate when exactly the clock should tick:

from datetime import datetime
import tkinter as tk
from time import time


def main() -> None:
    display = tk.Tk()
    display.geometry('215x62')
    display.title('Clock')

    lbl = tk.Label(
        display,
        background='black',
        font=('Helvetica', 37),
        foreground='red',
    )
    lbl.place(x=0, y=0)

    def tick() -> None:
        now = datetime.now()
        lbl.config(text=now.strftime('%H:%M %p'))

        until_next = round(
            1000 * (60 - time()%60)
        )
        display.after(ms=until_next, func=tick)

    tick()
    display.mainloop()


if __name__ == '__main__':
    main()
added 888 characters in body
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
Loading
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
Loading