Home > Software engineering >  is there a way to switch between 24h hour and 12hour am/pm using tkinter with 2 button widgets?
is there a way to switch between 24h hour and 12hour am/pm using tkinter with 2 button widgets?

Time:08-06

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

CodePudding user response:

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()
  • Related