Thursday, January 5, 2023

Musical Alarm

 I ended up creating a new thread for my musical alrm. So I close the clock and the

song keeps going. The alternative was to use sleep() which stops everything...



from turtle import *
import turtle
from datetime import datetime, timedelta
from playsound import playsound
import threading

def looks():
#Screen().bgcolor('Gray68')
Screen().bgcolor('#657F84')

def play():
playsound(r'C:\Users\Louise\Music\nonstopix-non-stop-rising-drum-beat-20804.mp3')

def alarm(x, y):
al_time = datetime.today() + timedelta(minutes=20)
my_alarm = al_time.strftime('%H:%M')
ht()
clockf.pu()
clockf.goto(200,180)
clockf.color('VioletRed3')
t = threading.Timer(20*60, play)
t.start()


clockf.write(my_alarm, font=("Ink_Free", 18, "bold", "italic"))
def jump(distanz, winkel=0):
penup()
right(winkel)
forward(distanz)
left(winkel)
pendown()

def hand(laenge, spitze):
fd(laenge*1.15)
rt(90)
fd(spitze/2.0)
lt(120)
fd(spitze)
lt(120)
fd(spitze)
lt(120)
fd(spitze/2.0)

def make_hand_shape(name, laenge, spitze):
reset()
jump(-laenge*0.15)
begin_poly()
hand(laenge, spitze)
end_poly()
hand_form = get_poly()
register_shape(name, hand_form)

def clockface(radius):
reset()
pensize(7)
pencolor('purple')
for i in range(60):
jump(radius)
if i % 5 == 0:
fd(25)
jump(-radius-25)
else:
dot(7)
jump(-radius)
rt(6)

def setup():
global second_hand, minute_hand, hour_hand, writer
mode("logo")
make_hand_shape("second_hand", 125, 25)
make_hand_shape("minute_hand", 130, 25)
make_hand_shape("hour_hand", 90, 25)
clockface(160)
minute_hand = Turtle()
minute_hand.shape("minute_hand")
minute_hand.color("brown", "magenta")
hour_hand = Turtle()
hour_hand.shape("hour_hand")
hour_hand.color("brown", "magenta")
second_hand = Turtle()
second_hand.shape("second_hand")
second_hand.color("gray20", "gray80")
for hand in second_hand, minute_hand, hour_hand:
hand.resizemode("user")
hand.shapesize(1, 1, 3)
hand.speed(0)
ht()
global writer
writer = Turtle()
#writer.mode("logo")
writer.ht()
writer.pu()
writer.bk(85)

def wochentag(t):
wochentag = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
return wochentag[t.weekday()]

def datum(z):
monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
"July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
j = z.year
m = monat[z.month - 1]
t = z.day
return "%s %d %d" % (m, t, j)

def tick():
t = datetime.today()
sekunde = t.second + t.microsecond*0.000001
minute = t.minute + sekunde/60.0
stunde = t.hour + minute/60.0
try:
tracer(False) # Terminator can occur here
writer.clear()
writer.home()
writer.forward(65)
writer.color('darkgreen')
writer.write(wochentag(t),
align="center", font=("Ink_Free", 14, "bold"))
writer.back(150)

writer.write(datum(t),
align="center", font=("Ink_Free", 14, "bold"))
writer.forward(85)
second_hand.setheading(6*sekunde) # or here
minute_hand.setheading(6*minute)
hour_hand.setheading(30*stunde)
tracer(True)
ontimer(tick, 100)
except Terminator:
pass # turtledemo user pressed STOP

def main():
tracer(False)
looks()
setup()
tracer(True)
tick()
return "EVENTLOOP"

if __name__ == "__main__":
mode("logo")
msg = main()
print(msg)
global clockf
clockf = turtle.Turtle()
clockf.speed(0)
clockf.ht()
clockf.backward(155)
clockf.right(90)
clockf.color('AntiqueWhite3')
clockf.fillcolor('AntiqueWhite3')
clockf.begin_fill()
clockf.circle(155)
clockf.end_fill()

onscreenclick(alarm)

mainloop()

No comments: