I'm new to python and I'm creating a countdown timer for an event with Python and I noticed that the image within the canvas doesn't fill up the entire screen on larger display's and the widgets don't resize when the window is shrunk. How do I fix this? Any help is appreciated. Thanks!
import datetime
import tkinter as tk
import winsound
def round_time(dt, round_to):
seconds = (dt - dt.min).seconds
rounding = (seconds + round_to / 2) // round_to * round_to
return dt + datetime.timedelta(0, rounding - seconds, -dt.microsecond)
def ct():
def count():
now = round_time(datetime.datetime.now(), round_to=1)
eh = datetime.datetime(2019, 3, 30, 20, 30)
tte = eh - now
canvas.itemconfig(label_cd, text=str(tte))
root.after(50, count)
count()
root = tk.Tk()
winsound.PlaySound('Sound1.wav', winsound.SND_ASYNC + winsound.SND_LOOP)
root.bind('<space>', lambda a: winsound.PlaySound(None, winsound.SND_PURGE))
root.bind('<s>', lambda a: winsound.PlaySound('Sound1.wav', winsound.SND_ASYNC + winsound.SND_LOOP))
root.geometry('1920x1080')
root.attributes('-fullscreen', True)
root.bind('<Escape>', lambda e: root.attributes("-fullscreen", False))
root.bind('<F11>', lambda z: root.attributes("-fullscreen", True))
root.title("Earth Hour Countdown!")
now = round_time(datetime.datetime.now(), round_to=1)
eh = datetime.datetime(2019, 3, 30, 20, 30)
tte = eh - now
canvas = tk.Canvas(root, height=1000, width=600, highlightthickness=0)
canvas.place(relheight=1, relwidth=1)
bg_img = tk.PhotoImage(file="new.gif")
bg_label = canvas.create_image((682.5, 384), image=bg_img)
cte_logo = tk.PhotoImage(file="cte1.gif")
cte_label = canvas.create_image((690, 120), image=cte_logo)
logo = tk.PhotoImage(file="eh60.gif")
logo_canvas = canvas.create_image((715, 590), image=logo)
label_msg = canvas.create_text((410, 300), text="Earth Hour Countdown:", font="Calibri 60 bold", fill="#CDCEDF")
label_cd = canvas.create_text((1080, 300), text=str(tte), font="Calibri 60 bold", fill="#CDCEDF")
ehtime_label = canvas.create_text((700, 400), text=("Earth Hour: " + eh.strftime("%d-%m-%Y %H:%M:%S")), font="Calibri 60 bold", fill="#CDCEDF")
ct()
root.mainloop()
- Expected results: https://imgur.com/LkrbJa0
- Result on larger display: https://imgur.com/MtAYSIs
- Result when window is shrunk: https://imgur.com/bAjst6L