Code:
LINE_HEIGHT = 35
INSTRUCTION_BOX_WIDTH = 40
INSTRUCTION_BOX_HEIGHT = 26
INSTRUCTION_BOX_SPACING = 10
LEFT_MARGIN = 40
TOP_MARGIN = 5


class ProgramView(Canvas):
    def __init__(self, parent: Tk) -> None:
        Canvas.__init__(self, parent, height=2 * TOP_MARGIN + 4 * LINE_HEIGHT, highlightthickness=0)

    def redraw(
        self,
        program: Program,
        current_subprogram: Optional[List[str]],
        current_instruction_index: int,
    ) -> None:
        height = self.winfo_height()
        width = self.winfo_width()

        self.delete(ALL)
        self.create_rectangle(0, 0, width, height, fill=window_background_color, width=0)


        # boucle pour les 4 sous-programmes P1 à P4
        for i, subprogram in enumerate(
            [program.P1, program.P2, program.P3, program.P4]
        ):

            # dessin du titre
            instruction_center_y = TOP_MARGIN + i * LINE_HEIGHT + LINE_HEIGHT // 2
            self.create_text(LEFT_MARGIN // 3, instruction_center_y, text=f"P{i + 1}:")

            # dessin de chaque instruction
            for j, instr in enumerate(subprogram):
                instruction_center_x = (
                    LEFT_MARGIN
                    + j * (INSTRUCTION_BOX_SPACING + INSTRUCTION_BOX_WIDTH)
                    + INSTRUCTION_BOX_WIDTH // 2
                )
                instruction_x = instruction_center_x - INSTRUCTION_BOX_WIDTH // 2
                instruction_y = instruction_center_y - INSTRUCTION_BOX_HEIGHT // 2
                instruction_width = INSTRUCTION_BOX_WIDTH
                instruction_height = INSTRUCTION_BOX_HEIGHT

                # paramètres d'affichage
                background_color = "white"
                stroke_color = "grey"
                stroke_width = 1
                if subprogram is current_subprogram and j == current_instruction_index:
                    background_color = "lightgrey"
                    stroke_width = 3
                    stroke_color = "lightgrey"

                self.create_rectangle(
                    instruction_x,
                    instruction_y,
                    instruction_x + instruction_width,
                    instruction_y + instruction_height,
                    outline=stroke_color,
                    width=stroke_width,
                    fill=background_color,
                )

                self.create_text(
                    instruction_center_x, instruction_center_y, text=str(instr)
                )
Last modified: Thursday, 5 December 2019, 3:58 PM