使用Tkinter,矩阵和像素的Python中的LED板

时间:2016-07-11 04:44:12

标签: python tkinter

作为Python项目的一部分,我正在尝试使用 Tkinter 库制作LED板。该委员会应收到一个输入(一个句子)并在董事会上显示。我已经开始为每个字母制作var options = { scales: { yAxes: [ { ticks: { callback: function(label, index, labels) { return label/1000+'k'; } }, scaleLabel: { display: true, labelString: '1k = 1000' } } ] } } 个零和一个矩阵的矩阵,但我真的不知道下一步该怎么做以及这些字母应该如何在板上显示。

例如:

7*6

信件应如下所示: enter image description here

1 个答案:

答案 0 :(得分:0)

示例代码

import tkinter as tk

# Should have constants in another .py or .json file 
DOT_WIDTH = 20
DOT_HEIGHT = 20

LETTER_WIDTH = 6 * DOT_WIDTH
LETTER_HEIGHT = 7 * DOT_HEIGHT

LETTERS = { 'A' :
                (
                    (0,1,1,1,0,0),
                    (1,0,0,0,1,0),
                    (1,0,0,0,1,0),
                    (1,0,0,0,1,0),
                    (1,1,1,1,1,0),
                    (1,0,0,0,1,0),
                    (1,0,0,0,1,0)
                ),
            'B' :
                (
                    (1,1,1,1,0,0),
                    (1,0,0,0,1,0),
                    (1,0,0,0,1,0),
                    (1,1,1,1,0,0),
                    (1,0,0,0,1,0),
                    (1,0,0,0,1,0),
                    (1,1,1,1,1,0)
                )
            }

class LED(tk.Canvas):
    def __init__(self, master):
        super().__init__(master, width = 500, height = 300, bg = 'black')

        self._cursor_pos = 2
        self._line_pos = 2

    def draw_letter(self, char):
        for i, row in enumerate(LETTERS.get(char, ())):
            for j, col in enumerate(row):
                if col:
                    self.create_oval( self._cursor_pos + j * DOT_WIDTH,
                                      self._line_pos + i * DOT_HEIGHT,
                                      self._cursor_pos + (j + 1) * DOT_WIDTH,
                                      self._line_pos + (i + 1) * DOT_HEIGHT,
                                      fill = 'red'
                                    )

        self._cursor_pos += LETTER_WIDTH

    def new_line(self, size):
        if self._cursor_pos + size >= self.winfo_width():
            self._cursor_pos = 2
            self._line_pos += LETTER_HEIGHT

class App(object):
    def __init__(self, master):
        self._led = LED(master)
        self._led.pack(expand = True, fill = tk.BOTH)

        entry = tk.Entry(master)
        entry.pack(fill = tk.X)
        entry.bind("<Return>", lambda e: self.draw_sentence(entry.get()))

    def draw_sentence(self, s):
        for word in s.split():
            self._led.new_line(len(word) * LETTER_WIDTH)  
            for char in word:
                self._led.draw_letter(char.upper())
            self._led._cursor_pos += LETTER_WIDTH//2 

if __name__ == '__main__':
    root = tk.Tk()
    app = App(root)
    root.mainloop()