7段显示时间

时间:2019-01-02 09:16:01

标签: python user-interface tkinter display

我想创建一个包含7段显示的GUI。我需要能够使3个显示器彼此相邻。 我的问题基本上是这样的:Seven segment display in Tkinter

但是,我无法在其旁边添加另一个显示。我知道,如果更改偏移量,它将移动显示内容,但似乎与添加在其旁边的另一个显示无关。

我知道这是重复一个问题,但是我无法对原始帖子发表评论。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您可以创建具有不同偏移量的类Digit的另一个对象。这是一个例子。

import tkinter as tk
root = tk.Tk()
screen = tk.Canvas(root)
screen.grid()

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
)

class Digit:
    def __init__(self, canvas, x=10, y=10, length=20, width=4):
        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')

dig = Digit(screen, 10, 10) ##
dig1 = Digit(screen, 40, 10) ##
n = 0
def update():
    global n
    dig.show(n)
    dig1.show(n) ## Control what you want to show here , eg (n+1)%10
    n = (n+1) % 10
    root.after(1000, update)
root.after(1000, update)
root.mainloop()

enter image description here