I am trying to create a simple app that switches between 24h (military) and 12 hour with am/pm (standard) time. I am using Tkinter with 2 button widgets, so If I press the standard time button the app would switch to standard time and disable the standard time button and enable the military time button and if I press the military time button the app would switch to military time and the military button would be disabled and enable standard time button.

The problem I am facing is that when I switch to military time by clicking on the button widget on the app, it starts to go haywire and I am unable to switch back to standard time so i have hit a roadblock here. I have 2 functions each for when one of the buttons whenever they are pressed the functions are called,display_standard_time() and display_military_time(). I have also tried using only one function in which to enable and disable switches and the results are the same as mentioned above.

The code below is what I currently have:

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

推荐答案

You should separate the clock update into another function and use .after() on that function instead:

import tkinter as tk
from tkinter import ttk, Label
from time import strftime

# function to update the clock periodically
def update_clock():
    display_clock['text'] = strftime(time_fmt)
    display_clock.after(1000, update_clock)

def display_military_time():
    global time_fmt # store the clock format
    time_fmt = '%H:%M:%S %p\n%b %d, %Y'
    military_time_button["state"] = "disabled"
    standard_time_button["state"] = "normal"

def display_standard_time():
    global time_fmt
    time_fmt = '%I:%M:%S %p\n%b %d, %Y'
    military_time_button["state"] = "normal"
    standard_time_button["state"] = "disabled"

if __name__ == '__main__':

    main_window = tk.Tk()

    main_window.title('Digital Clock')
    main_window.geometry('800x600')
    main_window.resizable(False, False)
    tabControl = ttk.Notebook(main_window)
    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() # set initial clock format
    update_clock() # start updating clock
    main_window.mainloop()

Python相关问答推荐

将整组数组拆分为最小值与最大值之和的子数组

将两只Pandas rame乘以指数

处理带有间隙(空)的duckDB上的重复副本并有效填充它们

如何使用它?

avxspan与pandas period_range

在Python argparse包中添加formatter_class MetavarTypeHelpFormatter时, - help不再工作""""

在含噪声的3D点网格中识别4连通点模式

导入...从...混乱

如果初始groupby找不到满足掩码条件的第一行,我如何更改groupby列,以找到它?

在输入行运行时停止代码

如何使用使用来自其他列的值的公式更新一个rabrame列?

如何找出Pandas 图中的连续空值(NaN)?

搜索按钮不工作,Python tkinter

Gekko中基于时间的间隔约束

需要帮助使用Python中的Google的People API更新联系人的多个字段'

ModuleNotFoundError:Python中没有名为google的模块''

如何编辑此代码,使其从多个EXCEL文件的特定工作表中提取数据以显示在单独的文件中

Polars时间戳同步延迟计算

按最大属性值Django对对象进行排序

更新包含整数范围的列表中的第一个元素