SigHound
SigHound

Reputation: 65

Trying to Swap Time from 24 format to 12 hour on GUI

I am trying to swap the outputted time back and forth between 24 and 12 hour formats. The time is ouputted on the gui as text. I have already created my interface, and the button but for some reason my function calls are not responding correctly.

This is my code so far:

import time
from tkinter import *
import os

class ConfigurationManagement():
    def __init__(self):
        self.__clockMode = 12
        self.__clockColor = ''
        self.__swapButtonTextColor = ''
        self.__swapButtonColor = ''

    def readClockSetting(self):
        cwd = os.getcwd()
        print(cwd)
        filename = "clockSetting.txt"
        setting = open(filename, 'r')
        allSetting = setting.readlines()
        setting.close()
        return allSetting

    def setClockMode(self, clockMode):
        self.__clockMode = clockMode

    def setClockColor(self, clockColor):
        self.__clockColor = clockColor

    def setSwapButtonTextColor(self, swapButtonTextColor):
        self.__swapButtonTextColor = swapButtonTextColor

    def setSwapButtonColor(self, swapButtonColor):
        self.__swapButtonColor = swapButtonColor

    def getClockMode(self):
        return self.__clockMode


def settingUpClockSetting():
    global clock
    clock = ConfigurationManagement()
    allSetting = clock.readClockSetting()

    clock.setClockMode(allSetting[3])
    clock.setClockColor(allSetting[6])
    clock.setSwapButtonTextColor(allSetting[9])

def timeIn24(pastTime=''):
    currentTime = time.strftime('%H: %M: %S')
    if currentTime != pastTime:
        digitalClock.config(text=currentTime)
    digitalClock.after(200, timeIn24)


def timeIn12(pastTime=''):
    currentTime = time.strftime('%I: %M: %S')
    if currentTime != pastTime:
        digitalClock.config(text=currentTime)
    digitalClock.after(200, timeIn24)

def clockSwap():
    print("Cat")
    clockMode = clock.getClockMode()
    if clockMode == 12:
        clock.setClockMode(24)
        timeIn24()
    elif clockMode == 24:
        clock.setClockMode(12)
        timeIn12()


settingUpClockSetting()
root = Tk()
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root).pack(side=BOTTOM)
digitalClock = Label(topFrame, font=('times', 100, 'bold'), bg='black', fg='green')
digitalClock.pack()
timeIn12()
root.geometry('700x500')
timeSwapButton = Button(bottomFrame, text="24/12 Modes", fg="red", bg="black", command=clockSwap).pack()
root.mainloop()

----------Digital Clock Configuration File----------

ClockType[12,24]:
12

ClockColor[The following colors can be handled: ]:
green

SwapButtonTextColor[The following colors can be handled ]:
red

SwapButtonColor[The following colors can be handled: ]:
black

I added the cat print to the clockswap function to make sure my button is actually working , and it for sure is.

Upvotes: 0

Views: 287

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385960

It looks like you keep scheduling new clock updates without stopping the old clock updates. Also, timeIn12 uses timeIn24 in the call to after.

You don't need two functions to do the updating. Just have a single function do the updating, and all it needs to do is change the format. For example:

def updateTime(self):
    if self.getClockMode() == 12:
        currentTime = time.strftime('%I: %M: %S')
    else
        currentTime = time.strftime('%H: %M: %S')
    ...

Upvotes: 2

SigHound
SigHound

Reputation: 65

I just got this fixed,

I added the following function:

def clockSwap():
    global counter
    if counter == 24:
        counter = 12
    elif counter == 12:
        counter += 12
    return counter

and added both clocks to the same counter:

def timeIn24Or12(pastTime=''):

if counter == 12:
    currentTime = time.strftime('%H: %M: %S')
    if currentTime != pastTime:
        digitalClock.config(text=currentTime)
    digitalClock.after(200, timeIn24Or12)
elif counter == 24:
    currentTime = time.strftime('%I: %M: %S')
    if currentTime != pastTime:
        digitalClock.config(text=currentTime)
    digitalClock.after(200, timeIn24Or12)

Upvotes: 0

Related Questions