尝试在GUI上将时间从24格式交换到12小时

问题描述 投票:0回答:2

我试图在24到12小时格式之间来回交换输出的时间。时间以gui作为文本输出。我已经创建了我的界面和按钮但由于某种原因我的函数调用没有正确响应。

到目前为止这是我的代码:

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

我将cat print添加到clockswap函数中以确保我的按钮实际工作,而且肯定是。

python user-interface time tkinter
2个回答
2
投票

您似乎在不停止旧时钟更新的情况下继续安排新的时钟更新。此外,timeIn12timeIn24的召唤中使用after

您不需要两个函数来进行更新。只需要一个函数来进行更新,它需要做的只是改变格式。例如:

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

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)
© www.soinside.com 2019 - 2024. All rights reserved.