JoshuaGuichelaar
Posts: 1
Joined: Tue Jul 17, 2018 6:41 pm

Python Alarm

Tue Jul 17, 2018 6:46 pm

I am making an alarm with python and ran into a problem I could not stop the alarm. Was just wondering if anyone has an idea on how to fix it?

import datetime
import time
from pygame import mixer
from tkinter import *


mixer.init()
mixer.music.load("/home/pi/Desktop/Audio/sound1.mp3")

root = Tk()
root.title('Alarm')
root.geometry('320x200+0+0')


label = Label(root, text='Set hour: ').place(x=0, y=10)
label = Label(root, text='Set minute: ').place(x=0, y=40)


L = StringVar()
Entery_Box = Entry(root, textvariable=L).place(x=140, y=10)


M = StringVar()
Entery_Box = Entry(root, textvariable=M).place(x=140, y=40)
while True:
x = datetime.datetime.now()

def Start():
hour = L.get()
munite = M.get()
while True:
print (x)
if x.hour == int(hour) and x.minute == int(munite) or x.minute == int(munite) +1 :
mixer.music.play()

def Stop():
sys.exit()
button = Button(root, text='Start Alarm', command = Start ).place(x=0, y=80)
button = Button(root, text='STOP Alarm', command = Stop ).place(x=0, y=100)

User avatar
paddyg
Posts: 2196
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python Alarm

Tue Jul 17, 2018 10:37 pm

You really need to use tk mainloop which has a facility for looping functions after so many milliseconds (30000 should ensure you don't miss the relevant start minute)

Code: Select all

import datetime
import time
from pygame import mixer
from tkinter import *


mixer.init()
mixer.music.load("temp3.mp3")

root = Tk()
root.title('Alarm')
root.geometry('320x200+0+0')

label = Label(root, text='Set hour: ').place(x=0, y=10)
label = Label(root, text='Set minute: ').place(x=0, y=40)

L = StringVar()
Entery_Box = Entry(root, textvariable=L).place(x=140, y=10)

M = StringVar()
Entery_Box = Entry(root, textvariable=M).place(x=140, y=40)

def check_time():
    x = datetime.datetime.now()
    hour = L.get()
    munite = M.get()
    print (x)
    if x.hour == int(hour) and x.minute == int(munite) or x.minute == int(munite) +1 :
        mixer.music.play()
    root.after(30000, check_time)

def stop_music():
    mixer.music.stop()

button = Button(root, text='Start Alarm', command = check_time).place(x=10, y=80)
button = Button(root, text='STOP Alarm', command = stop_music).place(x=10, y=120)
button = Button(root, text='Quit', command = root.destroy).place(x=10, y=160)

root.mainloop()
ps you are much more likely to get help if you use code tags as suggested in the stick at the start of this forum
PS as a challenge you should figure out the code to turn the alarm off after 30 minutes
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”

Who is online

Users browsing this forum: No registered users and 14 guests