【发布时间】:2019-04-17 16:03:57
【问题描述】:
我需要一个带有time.strftime 的七段时钟。我有这个时钟:
import sys
from tkinter import *
import time
def tick():
time_string = time.strftime("%M:%S")
clock.config(text=time_string)
clock.after(200, tick)
root = Tk()
clock = Label(root, font = ("times", 250, "bold"), bg="white")
clock.grid(row=0, column=1)
tick()
root.mainloop()
这是数字:
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
)
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, length=20, width=3):
self.canvas = canvas
l = length
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=width, 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')
但是,我不知道如何组合它们。
我们将不胜感激对实际代码的任何改进,如果您发现应该删除或替换某些内容,请随时进行更改。我不需要使用这个确切的实现,但这就是我所拥有的。如果库中有什么功能可以让工作变得更容易,请告诉我。
【问题讨论】:
-
为什么不能在 tkinter 中使用常规时间跨度?你想用 arduino 做点什么吗?
-
我需要七段代码,因为我有另一个 opencv 脚本来测试摄像机是否识别数字时钟。不过,我在下面的答案中得到了解决方案。
标签: python python-2.7 tkinter