gpt4 book ai didi

python - python Tkinter 中带有 strftime 的七段数字时钟与系统时间

转载 作者:行者123 更新时间:2023-12-01 08:45:52 25 4
gpt4 key购买 nike

我需要一个带有 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')

但是,我不知道如何将它们结合起来。

对实际代码的任何改进都将受到赞赏,如果您发现某些内容应该被删除或替换,请随时进行更改。我不需要使用这个确切的实现,但这就是我所拥有的。如果库中有一个函数或其他东西可以使工作变得更容易,请告诉我。

最佳答案

好的,我尝试根据您提供的代码将 strftime 转换为 7 段时钟。

我的目标是为格式化的 timestrafe 字符串的每个数字生成一个 Canvas 列表。然后,在时钟的每一圈,只需使用 Digit 类来更新每个 Canvas 项。

如果您有任何疑问,请告诉我。

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()

请记住,OP 的代码只是查看分钟/秒,因此该时钟将反射(reflect)这一点。

结果:

enter image description here

关于python - python Tkinter 中带有 strftime 的七段数字时钟与系统时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53304437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com