I am fairly new to programming and really new to programming in Python. And I have made a countdown timer for fun to try to learn programming. It looks like this:
I need help with to play an audio file when the counter is at zero. For example, when the 1 min button has been pressed, it needs to play an audio file when it reaches zero. The same goes for the other buttons. I don't know how to play audio in Python, and how to connect in a way so it plays when the program meets the requirement of the counter = "00:00:00". Here is my entire code:
import tkinter as tk
def update(seconds):
global after
if seconds >= 0:
countdown.set(seconds_to_time(seconds))
after = root.after(1000, lambda: update(seconds - 1))
else:
root.after_cancel(after)
def seconds_to_time(seconds):
hours = seconds // 3600
seconds -= hours * 3600
minutes = seconds // 60
seconds -= minutes * 60
return f'{hours:02d}:{minutes:02d}:{seconds:02d}'
def stop():
try:
root.after_cancel(after)
except NameError:
pass
#GUI
root = tk.Tk()
root.title("Tequila timer")
#Load of background with the tequila bottle
canvas = tk.Canvas(root, width=423, height=700)
canvas.pack()
Load = tk.PhotoImage(file="Flaske.png")
canvas.create_image(211, 350, image=Load)
countdown = tk.StringVar()
countdown.set("00:00:00")
#buttons
btn_10sek = tk.Button(root, text="10 sekunder", width=10, height=5, command=lambda: update(10))
btn_10sek_v = canvas.create_window(140, 300, window=btn_10sek)
btn_1min = tk.Button(root, text="1 min", width=10, height=5, command=lambda: update(60))
btn_1min_v = canvas.create_window(283, 300, window=btn_1min)
btn_10min = tk.Button(root, text="10 min", width=10, height=5, command=lambda: update(60 * 10))
bt1_10min_v = canvas.create_window(140, 425, window=btn_10min)
btn_1time = tk.Button(root, text="1 time", width=10, height=5, command=lambda: update(3600))
bt1_1time_v = canvas.create_window(283, 425, window=btn_1time)
btn_2timer = tk.Button(root, text="2 timer", width=10, height=5, command=lambda: update(7200))
bt1_2timer_v = canvas.create_window(140, 550, window=btn_2timer)
btn_stop = tk.Button(root, text="Stop tiden", width=10, height=5, command=stop)
bt1_stop_v = canvas.create_window(283, 550, window=btn_stop)
btn_luk = tk.Button(root, text="Luk ned", width=12, height=3, command=root.quit)
bt1_luk_v = canvas.create_window(211, 635, window=btn_luk)
#Display
label = tk.Label(root, textvariable=countdown, width=9, font=("arial black", 40, "bold"), fg="red")
label.pack()
label_v = canvas.create_window(211, 165, window=label)
#Overskrift
over = tk.Label(root, text="Tequila timer", font=("arial black", 40, "bold"), fg="red")
over.pack()
over_v = canvas.create_window(211, 30, window=over)
root.mainloop()
Update!!!
I figured it out. I used the simpleaudio library and made a if statement that said seconds < 0. What this does is that when the counter is done with counting down to zero, it plays the audio file because seconds is now below 0. I added into my time_function which is called "update(seconds", so it looks like this:
def update(seconds):
global after
if seconds >= 0:
countdown.set(seconds_to_time(seconds))
after = root.after(1000, lambda: update(seconds - 1))
else:
root.after_cancel(after)
if seconds < 0:
wave_obj = sa.WaveObject.from_wave_file("Te.wav")
play_obj = wave_obj.play()
play_obj.wait_done()