在GUI上尝试将时间从24小时格式转换为12小时

2024-09-28 20:52:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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打印添加到clockswap函数中,以确保我的按钮实际工作,而且确实工作。你知道吗


Tags: selfdefrootpackclockcurrenttimedigitalclockpasttime
2条回答

我刚把它修好

我添加了以下函数:

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)

看起来您一直在计划新的时钟更新,而没有停止旧的时钟更新。另外,timeIn12在对after的调用中使用timeIn24。你知道吗

更新不需要两个函数。只需使用一个函数进行更新,它所需要做的就是更改格式。例如:

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

相关问题 更多 >