1
我已經爲鬧鐘寫了下面的代碼,發現它在GitHub上。但在我的代碼中,它拋出了一個錯誤,指出標籤沒有被定義。Python ttk標籤錯誤,標籤undefined
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()
它說bottom_label沒有在dialog_box()中定義爲什麼會發生這種情況?
對不起,我不能很清楚地理解它,你能再解釋一下嗎? –
我希望現在很清楚。你也許想擺脫'while'循環。 –
嗯,我發現錯誤是在按鈕處理程序回調中,我在回調中使用了一個不必要的paraenthesis,in命令。我刪除它之後,該程序正在按照需要工作 –