python - 在 Python 中添加小时到时间
问题描述
我正在用 python 制作一个时钟,我有一个代码参考,我想显示时钟的小时、分钟和秒,而下面的代码只显示了上面 3 件事中的 2 个。%H%M%S
代码:
import tkinter as tk
import time
# Order 7 segments clockwise from top left, with crossbar last.
# Coordinates of each segment are (x0, y0, x1, y1)
# given as offsets from top left measured in segment lengths.
offsets = (
(0, 0, 1, 0), # top
(1, 0, 1, 1), # upper right
(1, 1, 1, 2), # lower right
(0, 2, 1, 2), # bottom
(0, 1, 0, 2), # lower left
(0, 0, 0, 1), # upper left
(0, 1, 1, 1), # middle
)
# Segments used for each digit; 0, 1 = off, on.
digits = (
(1, 1, 1, 1, 1, 1, 0), # 0
(0, 1, 1, 0, 0, 0, 0), # 1
(1, 1, 0, 1, 1, 0, 1), # 2
(1, 1, 1, 1, 0, 0, 1), # 3
(0, 1, 1, 0, 0, 1, 1), # 4
(1, 0, 1, 1, 0, 1, 1), # 5
(1, 0, 1, 1, 1, 1, 1), # 6
(1, 1, 1, 0, 0, 0, 0), # 7
(1, 1, 1, 1, 1, 1, 1), # 8
(1, 1, 1, 1, 0, 1, 1), # 9
(1, 1, 1, 0, 1, 1, 1), # 10=A
(0, 0, 1, 1, 1, 1, 1), # 11=b
(1, 0, 0, 1, 1, 1, 0), # 12=C
(0, 1, 1, 1, 1, 0, 1), # 13=d
(1, 0, 0, 1, 1, 1, 1), # 14=E
(1, 0, 0, 0, 1, 1, 1), # 15=F
)
class Digit:
def __init__(self, canvas, *, x=10, y=10, l=20, wt=3):
self.canvas = canvas
canvas.delete("all")
self.segs = []
for x0, y0, x1, y1 in offsets:
self.segs.append(canvas.create_line(x + x0*l, y + y0*l, x + x1*l, y + y1*l, width=wt, state='hidden'))
def show(self, num):
for iid, on in zip(self.segs, digits[num]):
self.canvas.itemconfigure(iid, state='normal' if on else 'hidden')
def tick():
global canvas_list
for ndex, num in enumerate(time.strftime("%M:%S").replace(':', '')):
Digit(canvas_list[ndex]).show(int(num))
root.after(1000, tick)
root = tk.Tk()
clock_frame = tk.Frame(root)
clock_frame.grid(row=1, column=0)
canvas_list = []
time_col = 0
canvas_count = 0
for i in range(5):
if i == 2:
tk.Label(clock_frame, text=":", font = ("times", 40, "bold")).grid(row=0, column=time_col)
time_col += 1
else:
canvas_list.append(tk.Canvas(clock_frame, width=30, height=50))
canvas_list[canvas_count].grid(row=0, column=time_col)
canvas_count += 1
time_col += 1
tick()
root.mainloop()
解决方案
推荐阅读
- google-tag-manager - 如何将 Google Tag Assistant 用于另一个 Chrome 扩展程序
- wordpress - 使用 graphql 在 WordPress 中获取帖子/页面中的所有块
- javascript - JQuery:如何选择包含在表内 TD 中的特定输入
- php - 如何在 php 中按值对数组进行切片或拼接?
- node.js - 当有一些节点实例时,如何正确处理 worker_threads 终止?(如 docker-swarm 或 Kubernetes 集群)
- perl - 为什么 Perl 的 Devel::Cover 认为有些分支和条件没有被覆盖?
- time - Youtrack 和 Jira 的“2d 4h 30m”时间段格式有名称吗?
- java - plantext模式下的Kafka,客户端需要在properties中设置哪些字段才能创建producer?(Java程序中)
- r - 使用 ggplot2 对两条直线之间的区域进行着色
- reactjs - 反应 + 烧瓶 ec2 部署