0

I have written the following code for an alarm clock, found it on GitHub. But in my code it throws up an error saying that the label is not defined.

import time as time, os as os
from tkinter import *
from tkinter import messagebox
from tkinter import ttk  # For buttons, labels, frames etc...

root = Tk()  # Creates the tkinter object root
root.title('Alarm Clock')  # Sets the window title
root.geometry('200x200')  # Sets the window dimension


def submit_button():  # The core condition for the alarm clock
     alarm_time = time_entry.get()
     dialog_box()
     #bottom_label.config(text ="The Alarm will Ring at {} ".format(AlarmTime))  #delayed in execution
     current_time = time.strftime('%H:%M')  # Sets the current_time format to Hours:Minutes
     print('The alarm time is: {}' .format(alarm_time))
     while alarm_time != current_time:
         current_time = time.strftime('%H:%M')  # Sets the current_time format to Hours:Minutes
         time.sleep(1)  # To give a delay in while loop 
     if alarm_time == current_time:  # When current_time is alarm time
         os.system('start alarm-music.mp3')  # Play the music from the os
         bottom_label.config(text="Alarm music playing....")
         messagebox.showinfo(title='Alarm Message', message="{}".format(entry2.get()))


def dialog_box():
    alarm_time = time_entry.get()
    messagebox.showinfo("Alarm Clock", "alarm will ring at {}" .format(alarm_time))  # title and display of dialog box.
    bottom_label.config(text = "The alarm time is counting...")  # configures the text below submit button


# Frame creation
alarm_frame = ttk.Frame(root)  # Creates a frame named alarm_frame
alarm_frame.config(height=200, width=200)  # Sets the dimensions of the frame
alarm_frame.pack()

# Alarm Label
alarm_label = ttk.Label(alarm_frame, text = "Enter the alarm time:")  # Creates a label with text Enter alarm time
alarm_label.pack()  # Unpacks the label onto the frame

# Entering time
time_entry = ttk.Entry(alarm_frame, width=30)  
time_entry.insert(3, 'example: 13:15')  
time_entry.pack()  # Unpacks the entry on the frame

 # Message label
message_label = ttk.Label(alarm_frame, text = "Enter message to display:")
message_label.pack()

# Entering Message
message_entry = ttk.Entry(alarm_frame, width = 30)  # Facilitates entering    alarm message
message_entry.pack()  # Unpacks the entry area

# Creation of submit button
submit = ttk.Button(alarm_frame, text = 'submit', command = submit_button())     
submit.pack()  # Unpacks the button onto the frame


# Bottom label creation
bottom_label = ttk.Label(alarm_frame)  # Label(master, **kw)
bottom_label.pack()  # unpacks the bottom_label on the frame

root.mainloop()  

It says that bottom_label is not defined in dialog_box() why is this happening?

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Suraj Rao
  • 11
  • 4

1 Answers1

0

Use:

...
# Bottom label creation
bottom_label = ttk.Label(alarm_frame)  # Label(master, **kw)
bottom_label.pack()  # unpacks the bottom_label on the frame

# Creation of submit button
submit = ttk.Button(alarm_frame, text = 'submit', command = submit_button())     
submit.pack()  # Unpacks the button onto the frame
...

Instead of:

...
# Creation of submit button
submit = ttk.Button(alarm_frame, text = 'submit', command = submit_button())     
submit.pack()  # Unpacks the button onto the frame


# Bottom label creation
bottom_label = ttk.Label(alarm_frame)  # Label(master, **kw)
bottom_label.pack()  # unpacks the bottom_label on the frame
...

Order matters, you have to initialize bottom_label before using it in command=.

Juan T
  • 1,219
  • 1
  • 10
  • 21
  • Sorry I could not understand it quite clearly, could you explain a little more please? – Suraj Rao Feb 10 '17 at 18:05
  • I hope it's clear now. Also you may want get rid of that `while` loop. – Juan T Feb 10 '17 at 19:54
  • Well I figured out that the error was in button handler call back, I have used an unecessary paraenthesis in the call back, in command. After I removed it, the program is working as desired – Suraj Rao Feb 15 '17 at 08:40