def __init__(self, title: str, text: str):
        super().__init__(TK_ROOT)
        self.withdraw()
        self.title(title)
        self.transient(master=TK_ROOT)
        self.resizable(width=True, height=True)
        tk_tools.set_window_icon(self)

        # Hide when the exit button is pressed, or Escape
        # on the keyboard.
        self.protocol("WM_DELETE_WINDOW", self.withdraw)
        self.bind("<Escape>", self.withdraw)

        frame = tk.Frame(self, background='white')
        frame.grid(row=0, column=0, sticky='nsew')
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)

        self.textbox = tkRichText(frame, width=80, height=24)
        self.textbox.configure(background='white', relief='flat')
        self.textbox.grid(row=0, column=0, sticky='nsew')
        frame.grid_columnconfigure(0, weight=1)
        frame.grid_rowconfigure(0, weight=1)

        scrollbox = HidingScroll(
            frame,
            orient='vertical',
            command=self.textbox.yview,
        )
        scrollbox.grid(row=0, column=1, sticky='ns')
        self.textbox['yscrollcommand'] = scrollbox.set

        parsed_text = tkMarkdown.convert(text)
        self.textbox.set_text(parsed_text)

        ttk.Button(
            frame,
            text=_('Close'),
            command=self.withdraw,
        ).grid(
            row=1,
            column=0,
        )
Example #2
0
def init_widgets(master: ttk.Frame) -> Optional[tk.Misc]:
    """Construct the widgets, returning the configuration button.

    If no signages are defined, this returns None.
    """

    if not any(Signage.all()):
        return ttk.Label(master)

    window.protocol("WM_DELETE_WINDOW", window.withdraw)
    window.resizable(True, True)
    window.title(_('Configure Signage'))

    frame_selected = ttk.Labelframe(
        window,
        text=_('Selected'),
        relief='raised',
        labelanchor='n',
    )

    canv_all = tk.Canvas(window)

    scroll = HidingScroll(window, orient='vertical', command=canv_all.yview)
    canv_all['yscrollcommand'] = scroll.set

    name_label = ttk.Label(window, text='', justify='center')

    frame_selected.grid(row=0, column=0, sticky='nsew')
    name_label.grid(row=1, column=0, sticky='ew')
    canv_all.grid(row=0, column=1, rowspan=2, sticky='nsew')
    scroll.grid(row=0, column=2, rowspan=2, sticky='ns')
    window.columnconfigure(1, weight=1)
    window.rowconfigure(0, weight=1)

    utils.add_mousewheel(canv_all, canv_all, window)

    def on_hover(slot: dragdrop.Slot[Signage]) -> None:
        name_label['text'] = (_('Signage: {}').format(slot.contents.name)
                              if slot.contents is not None else '')

    def on_leave(slot: dragdrop.Slot[Signage]) -> None:
        name_label['text'] = ''

    drag_man.reg_callback(dragdrop.Event.HOVER_ENTER, on_hover)
    drag_man.reg_callback(dragdrop.Event.HOVER_EXIT, on_leave)

    for i in range(3, 31):
        SLOTS_SELECTED[i] = slot = drag_man.slot(frame_selected,
                                                 source=False,
                                                 label=f'00:{i:02g}')
        row, col = divmod(i - 3, 4)
        slot.grid(row=row, column=col, padx=1, pady=1)

        prev_id = DEFAULT_IDS.get(i, '')
        if prev_id:
            try:
                slot.contents = Signage.by_id(prev_id)
            except KeyError:
                LOGGER.warning('Missing sign id: {}', prev_id)

    for sign in sorted(Signage.all(), key=lambda s: s.name):
        if not sign.hidden:
            slot = drag_man.slot(canv_all, source=True)
            slot.contents = sign

    drag_man.flow_slots(canv_all, drag_man.sources())

    canv_all.bind(
        '<Configure>',
        lambda e: drag_man.flow_slots(canv_all, drag_man.sources()),
    )

    def show_window() -> None:
        """Show the window."""
        window.deiconify()
        utils.center_win(window, TK_ROOT)

    return ttk.Button(
        master,
        text=_('Configure Signage'),
        command=show_window,
    )