Python Forum

Full Version: My Attempt at an alarm clock
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My first go at an alarm clock
# /usr/bin/env python3

import tkinter as tk
from tkinter import ttk, messagebox
from time import strftime
from functools import partial

class Clock:
    def __init__(self, master):
        self.master = master
        self.master.rowconfigure(0, weight=1)
        self.master.columnconfigure(0, weight=1)

        # Do the frames
        container_style = ttk.Style()
        container_style.configure('Container.TFrame')
        self.container = ttk.Frame(self.master, style='Container.TFrame')
        self.container.grid(column=0, row=0, sticky='news')
        self.container.grid_columnconfigure(0, weight=2)

        clock_frame_style = ttk.Style()
        clock_frame_style.configure('ClockF')
        self.clock_frame = ttk.Frame(self.container, style='ClockF.TFrame')
        self.clock_frame.grid(column=0, row=0, sticky='new')
        self.clock_frame.grid_columnconfigure(0, weight=2)

        alframe_style = ttk.Style()
        alframe_style.configure('AL.TFrame', background='slategray')
        self.alarm_frame = ttk.Frame(self.container, border=3, relief='ridge', style='AL.TFrame')
        self.alarm_frame.grid(column=0, row=1, sticky='nw')
        self.alarm_frame.grid_columnconfigure(0, weight=3)

        self.clock()
        self.update_time()
        self.alarm_set()
        self.poll()




    def clock(self):
        style = ttk.Style()
        style.configure('Clock.TLabel', background='black', foreground='red', anchor='center', \
                        font=('sans', 16, 'bold'))
        self.face = ttk.Label(self.clock_frame, style='Clock.TLabel', border=3, relief='ridge')
        self.face.grid(column=0, row=0, sticky='ew', ipadx=10, ipady=2)
        self.face.grid_columnconfigure(0, weight=2)
        self.face['text'] = strftime('%I:%M:%S %p')

    def display_time(self):
        self.face['text'] = strftime('%I:%M:%S %p')

    def update_time(self):
        self.display_time()
        self.face.after(1000, self.update_time)

    def alarm_set(self):
        style = ttk.Style()
        style.configure('ALabel.TLabel', padding=3, background='slategray')

        hour_txt = ttk.Label(self.alarm_frame, text='Hour', style='ALabel.TLabel')
        hour_txt.grid(column=0, row=0, sticky='w')
        self.hour_set = tk.Spinbox(self.alarm_frame, from_=1, to=12, width=2)
        self.hour_set.grid(column=1, row=0, sticky='w', padx=3)

        spacer_style = ttk.Style()
        spacer_style.configure('Spacer.TLabel', background='slategray')
        spacer = ttk.Label(self.alarm_frame, width=2, style='Spacer.TLabel')
        spacer.grid(column=2, row=0)

        minute_txt = ttk.Label(self.alarm_frame, text='Min.', style='ALabel.TLabel')
        minute_txt.grid(column=3, row=0, sticky='w')
        self.minute_set = tk.Spinbox(self.alarm_frame, from_=0, to=59, width=2)
        self.minute_set.grid(column=4, row=0, sticky='w', padx=8, pady=4)

        spacer2 = ttk.Label(self.alarm_frame, relief='groove', style='Spacer.TLabel')
        spacer2.grid(column=0, row=1, columnspan=5, sticky='ew')

        self.myvars = tk.StringVar()

        chkbox_text = ttk.Label(self.alarm_frame, text='Alarm On/Off', style='Spacer.TLabel')
        chkbox_text.grid(column=0, row=2, columnspan=3, sticky='w', padx=3)
        self.chkbox = tk.Checkbutton(self.alarm_frame, variable=self.myvars, onvalue='alarm_on', \
                                     offvalue='alarm_off', bg='slategray', highlightcolor='slategray', \
                                highlightbackground='slategray')
        self.chkbox.deselect()
        self.chkbox.grid(column=3, row=2)


    def alarm_get(self):
        alarm = self.myvars.get()
        hour = self.hour_set.get()
        minute = self.minute_set.get()
        now_hour = strftime('%-I')
        now_minute = strftime('%-M')
        now_seconds = strftime('%S')

        if alarm == 'alarm_on':
            if hour == now_hour and minute == now_minute and now_seconds == '00':
                msg = messagebox.showinfo(title='Alarm', message='Alarm Works!')


        else:
            pass



    def poll(self):
        self.alarm_get()
        self.chkbox.after(1000, self.poll)





def main():
    root = tk.Tk()
    root.title('Alarm Clock')

    Clock(root)
    root.mainloop()

if __name__ == '__main__':
    main()