Python 3.x Python ttk标签错误,标签未定义
我已经为一个闹钟编写了以下代码,在GitHub上找到了它。但是在我的代码中,它抛出了一个错误,表示标签没有定义Python 3.x Python ttk标签错误,标签未定义,python-3.x,tkinter,Python 3.x,Tkinter,我已经为一个闹钟编写了以下代码,在GitHub上找到了它。但是在我的代码中,它抛出了一个错误,表示标签没有定义 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 r
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 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
...
而不是:
...
# 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
...
订单事宜,在
命令=
中使用它之前,您必须初始化bottom\u标签
对不起,我不能很清楚地理解它,请您再解释一下好吗?我希望现在已经清楚了。另外,您可能希望摆脱while
循环。我发现错误出现在按钮处理程序回调中,我在回调中使用了一个不必要的paratenthesis,in命令。在我删除它之后,程序按预期工作