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相关问答推荐

python 全局运算符仿真

如何聚合一个数据框然后用 Pandas 转置它

如何编写这个 for 循环以在 Python 中更快地处理?

如何对每条“HELLO THERE WORLD”行进行分组?

一对一字段的 Django Import-Export 导入错误 - KeyError: 'id'

Python - 多处理嵌套字典

Django QuerySet 猴子修补以向返回的模型添加新属性

如何在 tensorFlow 和 keras 中为这样的输入和输出示例配置布局

为什么我收到 TypeError:dtype datetime64[ns] 无法转换为 timedelta64[ns]?

Kivy 按钮无法关闭弹出窗口

如何调试 try except 块内的异常?

如何强制 PySpark 舍入以使用银行家舍入

如何将 pandas 数据框中的行作为同一行中的新行加入?

查找所有列的第一个值小于阈值的索引

如何在 OpenGL 中为我的对象上的每个面设置单独的 colored颜色 ?

单个位置索引越界 - Python

无法从网页中检索链接

SAS 中的函数

如何在非常大的 torch 张量上执行操作而不分裂它们

如何分组并获得最频繁的ngram?