2017-12-15 14 views
0

출력 된 시간을 24 시간에서 12 시간 형식으로 앞뒤로 전환하려고합니다. 시간은 텍스트로 gui에 ouputted입니다. 내 인터페이스와 버튼을 이미 만들었지 만 어떤 이유로 함수 호출이 올바르게 응답하지 않습니다.GUI에서 24 형식에서 12 시간으로 시간을 바꾼다.

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

---------- 디지털 시계 구성 파일 ----------

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 :이 지금까지 내 코드입니다

고양이 단추를 clockswap 함수에 추가하여 제 단추가 실제로 작동하는지 확인했습니다.

+0

디버거 사용을 고려 했습니까? 또는 실패한 경우 clockswap() 함수에서 if의 두 가지 분기 내에서 지문을 추가하거나 clock.getClockMode()에서 반환 한 값을 인쇄하려고 시도 했습니까? 이 간단한 디버깅 옵션을 시도하지 않았다면 StackOverflow에서 어떤 질문을하고 계십니까? – barny

+0

응답 해 주셔서 감사합니다. 실제로 문제는 잠시 나 자신을 수정하고 게시물에 새로운 대답을 추가했습니다. :); 상관없이 도와 줘서 고마워! –

+0

문제의 적어도 일부는''clockSetting.txt ''설정 파일 처리와 관련이 있습니다. 예를 들어'allSetting [3]'이 정수 '12'가 아닌 '12 \ n '문자열로 설정되어 있습니다. – martineau

답변

2

것 같습니다 오래된 시계 업데이트를 중단하지 않고 시계를 업데이트 할 수 있습니다. 또한 timeIn12after으로 전화 할 때 timeIn24을 사용합니다.

업데이트를 수행하는 데 두 가지 기능이 필요하지 않습니다. 하나의 함수 만 업데이트하면되므로 형식을 변경하면됩니다. 예 :

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

나는이 문제를 이미 해결했다. (질문을 어떻게 닫을 지 모르겠다.) 나의 최근 코멘트를 참조 할 수있다. 내가이 문제를 해결하기 위해 똑같이했기 때문에 당신의 답을 답으로 표시하든간에 –

0

난 그냥이 고정되었다가,

나는 다음과 같은 기능 추가 :

def clockSwap(): 
    global counter 
    if counter == 24: 
     counter = 12 
    elif counter == 12: 
     counter += 12 
    return 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)