python - 7段显示tkinter

标签 python user-interface tkinter display

我想创建一个包含 7 段显示器的 GUI。我需要能够让 3 个显示器并排放置。 我的问题基本上是这样的:Seven segment display in 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()

enter image description here

关于python - 7段显示tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54003781/

相关文章:

python - Pandas 如何将列表的列变成多列?

python - NLTK 单词与 word_tokenize

python - opencv 锐界提取图像

wpf - 创建 WPF UI...如果在 VS2008 中使用设计器,我应该麻烦吗?

c# - 低级对象上的 DependencyProperty/INotifyPropertyChanged

python - tkinter TreeView : get selected item values

python - 如何从 ttk.Treeview 小部件中清除项目?

python - 将字符插入字符串到数字末尾

python - 找不到 PyInstaller 库

Qt 设计师 : How to add widget to a layout in the designer when the layout appears infinitely thin?