簡體   English   中英

7段顯示時間

[英]7 Segment display tkinter

我想創建一個包含7段顯示的GUI。 我需要能夠使3個顯示器彼此相鄰。 我的問題基本上是這樣的: Tkinter中的七段顯示

但是,我無法解決在其旁邊添加另一個顯示的問題。 我知道,如果更改偏移量,它將移動顯示內容,但與添加在其旁邊的另一個顯示內容似乎無關。

我知道這是重復一個問題,但是我無法對原始帖子發表評論。

任何幫助將不勝感激。

您可以創建具有不同偏移量的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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM