Py学习  »  Python

我用python制作时钟时出错了

David • 2 年前 • 498 次点击  
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:\Users\david\OneDrive\Desktop\practice projects\clock.py", line 16, in <module>
    time()
  File "c:\Users\david\OneDrive\Desktop\practice projects\clock.py", line 10, in time
    string = strftime('%H:%M:%S %P')
ValueError: Invalid format string
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130378
 
498 次点击  
文章 [ 1 ]  |  最新文章 2 年前
Matt
Reply   •   1 楼
Matt    2 年前

试试小写字母P:

string = strftime('%H:%M:%S %p')

但这是24小时制,所以你可能不需要上午或下午。如果您想要12小时制,请使用:

string = strftime('%I:%M:%S %p')

文件: https://www.programiz.com/python-programming/datetime/strftime