from tkinter import *
from tkinter.ttk import *
from time import strftime
root = Tk()
root.title('Clock')
def time():
string = strftime('%H:%M:%S %P')
label.config(text=string)
label.after(1000, time)
label = Label(root, font=("ds-digital", 80), background="black", foreground="cyan")
label.pack(anchor='center')
time()
mainloop()
我收到这个错误
File "c:UsersdavidOneDriveDesktoppractice projectsclock.py", line 16, in <module>
time()
File "c:UsersdavidOneDriveDesktoppractice projectsclock.py", line 10, in time
string = strftime('%H:%M:%S %P')
ValueError: Invalid format string
尝试小写p:
string = strftime('%H:%M:%S %p')
但这是24小时制,所以你可能不需要上午或下午。如果你想要12小时制,请使用:
string = strftime('%I:%M:%S %p')
文档:https://www.programiz.com/python-programming/datetime/strftime
作为初学者,我在youtube教程中遇到了同样的错误,对我来说,修复方法很简单,将字符串中的p变成小写。("%H:%M:%S%p"(