Python Forum
My Attempt at an alarm clock
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My Attempt at an alarm clock
#1
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Playing around with a clock's seconds in tkinter menator01 1 2,478 Feb-12-2022, 04:32 PM
Last Post: menator01
  Simple wxpython digital clock menator01 2 4,072 Feb-20-2021, 05:47 PM
Last Post: menator01
  Binary Clock sparkz_alot 13 14,028 Jan-02-2017, 03:52 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020