我试图在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函数中,以确保我的按钮实际工作,而且确实工作。你知道吗
我刚把它修好
我添加了以下函数:
并将两个时钟添加到同一个计数器:
看起来您一直在计划新的时钟更新,而没有停止旧的时钟更新。另外,
timeIn12
在对after
的调用中使用timeIn24
。你知道吗更新不需要两个函数。只需使用一个函数进行更新,它所需要做的就是更改格式。例如:
相关问题 更多 >
编程相关推荐