我正试图创建一个简单的应用程序,在24小时(军事)和12小时之间切换的上午/下午(标准)时间.我正在使用带有2个按钮小工具的Tkinter,所以如果我按下标准时间按钮,应用程序将切换到标准时间按钮,禁用标准时间按钮,启用军事时间按钮,如果我按下军事时间按钮,应用程序将切换到军事时间,军事按钮将被禁用,并启用标准时间按钮.
我面临的问题是,当我点击应用程序上的按钮小工具切换到军事时间时,它开始失控,我无法切换回标准时间,所以我在这里遇到了障碍.我有两个函数,每个函数用于当一个按钮被按下时,函数被调用,display_standard_time()
和display_military_time()
.我也try 了只使用一个功能来启用和禁用switch ,结果与上面提到的相同.
下面的代码是我目前拥有的代码:
import tkinter as tk
from tkinter import ttk, Label #used to create buttons and widgets
from time import strftime
def display_military_time():
current_time = strftime('%H: '+'%M: '+'%S '+'%p') + \
'\n'+ strftime('%b '+ '%d')+ ',' + strftime('%Y')
display_clock.configure(text=current_time)
military_time_button["state"] = "disabled"#disable military_button
standard_time_button["state"] = "normal"#enable standard_button
display_clock.after(80, display_military_time) #this statement allows to execute display_military_time() after 100ms
def display_standard_time():
current_time = strftime('%I: ' + '%M: ' + '%S ' + '%p') + \
'\n'+ strftime('%b '+ '%d')+ ',' + strftime('%Y')
display_clock.configure(text=current_time)
standard_time_button["state"] = "disabled"#disable standard_button
military_time_button["state"] = "normal"#enable military_button
display_clock.after(80, display_standard_time)
if __name__ == '__main__':
main_window = tk.Tk()#Create an instance of tk.Tk class and create an application window
#Place label on window and write text in the window
main_window.title('Digital Clock') #Name of the window
main_window.geometry('800x600') #method to change the size and location of the window. , widthxheight±x±y
main_window.resizable(False, False)#Prevents window from being resized
tabControl = ttk.Notebook(main_window)#Initialize Tabs
clock_tab = ttk.Frame(tabControl)
tabControl.add(clock_tab, text='Clock')
tabControl.pack(expand=1, fill="both")
display_clock = Label(clock_tab,fg='black', font=("arial", 80))
display_clock.place(x=50, y=200)
clock_label = Label(clock_tab,fg='black',font=("arial", 60, 'bold'), text = 'Current Time')
clock_label.place(x=150, y=100)
military_time_button = tk.Button(clock_tab,text="MILITARY TIME",bd=10,bg="grey", fg="blue",command=display_military_time,activeforeground="Orange",
activebackground="blue",
font="Andalus",
height=2,
highlightcolor="purple",
justify="right",
state = 'normal')
military_time_button.place(x=100, y=470)
standard_time_button = tk.Button(clock_tab,text="STANDARD TIME",bd=10,bg="grey", fg="red",command=display_standard_time,activeforeground="Orange",
activebackground="blue",
font="Andalus",
height=2,
highlightcolor="purple",
justify="right",
state= 'disabled')
standard_time_button.place(x=500, y=470)
display_standard_time()
main_window.mainloop()#keeps the window displaying otherwise it disappears