Vai al contenuto

main_view

main_view

This module contains the main window of the application.

MainWindow

Bases: CTk

Main window of the application.

Source code in main_view.py
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
class MainWindow(ctk.CTk):
    """
    Main window of the application.
    """
    ICON: str = abspath(join(dirname(__file__), 'images/yt.ico'))
    LIVE_IMAGES: dict[str, str] = {
        'ready': abspath(join(dirname(__file__), 'images/live1.png')),
        'prepared': abspath(join(dirname(__file__), 'images/live2.png')),
        'live': abspath(join(dirname(__file__), 'images/live3.png'))
    }
    NO_CAMERA: str = abspath(join(dirname(__file__), 'images/no-camera.png'))
    COLORS = {
        'DEBUG': "black",
        'INFO': "blue",
        'WARN': "orange", 'WARNING': "orange",
        'ERROR': "red", 'CRITICAL': "red", 'FATAL': "red"
    }

    def __init__(self, presets: list[str]):
        """
        Initialize the main window.
        :param presets: The list of presets.
        """
        super().__init__()
        width, height = 750, 520
        self.geometry(f'{width}x{height}')
        self.title('Streaming Santa Croce')
        self.iconbitmap(self.ICON)
        self.resizable(False, False)

        self.__font = ('Arial', 14)
        self.__b_font = ('Arial', 15, 'bold')
        self.scale = self.winfo_width() / width
        self.__preset_callback_function: Callable[[str], None] = lambda x: None
        self.__images, self.__gui = self.__make_layout(presets)

    def __make_layout(self, presets: list[str]) \
            -> tuple[dict[str, ctk.CTkImage], dict[str, Any]]:
        """
        Create the layout of the main window.
        :param presets: The list of presets.

        :return: The images dictionary and the layout.
        """
        images = self.__load_images()
        gui = self.__make_live_frame(images['ready'])
        gui['presets'] = self.__make_presets_frame(presets)
        gui.update(self.__make_bottom_frame(images['no_camera']))
        gui.update(self.__make_status_bar())
        return images, gui

    @classmethod
    def __load_images(cls) -> dict[str, ctk.CTkImage]:
        """
        Load the images used in the window.

        :return: The images' dictionary.
        """
        images = {
            'ready': Image.open(cls.LIVE_IMAGES['ready']),
            'prepared': Image.open(cls.LIVE_IMAGES['prepared']),
            'live': Image.open(cls.LIVE_IMAGES['live']),
            'no_camera': Image.open(cls.NO_CAMERA)
        }
        scale = 0.95
        for img in ('ready', 'prepared', 'live'):
            images[img] = images[img].resize(
                (int(images[img].size[0] * scale),
                 int(images[img].size[1] * scale)), 1)
        scale = 0.17
        images['no_camera'] = images['no_camera'].resize(
            (int(images['no_camera'].size[0] * scale),
             int(images['no_camera'].size[1] * scale * 1.1)), 1)
        for img in images:
            images[img] = ctk.CTkImage(images[img], size=images[img].size)
        return images

    def __make_live_frame(self, image: ctk.CTkImage) -> dict[str, Any]:
        """
        Create the top frame that contains the live settings.
        :param image: The live image for the button.

        :return: The layout of the top frame.
        """
        live_frame = ctk.CTkFrame(self)
        live_frame.pack(fill='x', padx=5, pady=6)

        b1 = ctk.CTkButton(live_frame, image=image, text='', hover=False,
                           fg_color='transparent')
        b1.grid(row=0, column=0, rowspan=3, padx=(5, 0))
        gui = {'live_button': b1}

        l1 = ctk.CTkLabel(live_frame, text='Titolo:', font=self.__font)
        l1.grid(row=0, column=1, sticky='w', pady=5)
        l2 = ctk.CTkLabel(live_frame, text='Descrizione:', font=self.__font)
        l2.grid(row=1, column=1, rowspan=2, sticky='w',
                pady=(0, 5), padx=(0, 10))

        txt_var = ctk.StringVar()
        e1 = ctk.CTkEntry(live_frame, font=self.__font, border_width=1,
                          textvariable=txt_var)
        e1.grid(row=0, column=2, sticky="w", pady=5)
        gui['title'] = (txt_var, e1, e1.cget('fg_color'), e1.cget('text_color'))
        e2 = ctk.CTkTextbox(live_frame, font=self.__font, border_width=1,
                            height=70, wrap='word')
        e2.grid(row=1, column=2, rowspan=2, sticky="ns", pady=(0, 5))
        gui['description'] = e2

        l3 = ctk.CTkLabel(live_frame, text='Al termine', font=self.__font)
        l3.grid(row=0, column=3)
        l4 = ctk.CTkLabel(live_frame, text='00m 00s', font=('Arial', 25),
                          width=120)
        l4.grid(row=1, column=3, sticky='n', padx=10)
        gui['end_time'] = l4
        gui['time_button'] = ctk.CTkButton(live_frame, text='+ 10min', width=90,
                                           font=self.__b_font)
        gui['time_button'].grid(row=2, column=3, sticky='n')

        live_frame.update()
        lw = (live_frame.winfo_width() - int(e1.winfo_geometry().split('+')[1])
              - l4.winfo_width()) / self.scale - 20
        e1.configure(width=lw)
        e2.configure(width=lw)
        return gui

    def __make_presets_frame(self, presets: list) -> list[ctk.CTkButton]:
        """
        Create the frame that contains the presets buttons.
        :param presets: The list of presets.

        :return: The layout of the presets' frame.
        """
        preset_frame = ctk.CTkFrame(self)
        preset_frame.pack(fill='x', padx=5)
        preset_frame.update()
        frame_width = preset_frame.winfo_width()

        class PresetButton(ctk.CTkButton):  # pylint: disable=too-many-ancestors
            """
            A preset button.
            """

            def __init__(self, master, **kw):
                super().__init__(master, **kw)
                self._text_label.configure(wraplength=frame_width // 8)

        buttons = []
        for preset in presets:
            button = PresetButton(preset_frame, text=preset, width=80,
                                  height=45, font=self.__b_font)
            button.configure(command=lambda p=preset: self.__preset_callback(p))
            button.place(x=0, y=0)
            buttons.append(button)
        preset_frame.update()
        next_y = 10
        pad_x = 5
        for row in self.__organize_presets(buttons, frame_width,
                                           ceil(5 * self.scale)):
            next_x = row['pad'] / self.scale
            for button in row['buttons']:
                button.place(x=next_x, y=next_y)
                next_x += button.winfo_width() / self.scale + pad_x
            next_y += row['height'] / self.scale + 10
        preset_frame.configure(height=next_y)
        return buttons

    def __preset_callback(self, preset: str) -> None:
        """
        Call the given preset on the camera.
        :param preset: The preset name to call.
        """
        try:
            self.__preset_callback_function(preset)
        except AttributeError:
            pass

    @staticmethod
    def __organize_presets(buttons: list[ctk.CTkButton], width: int,
                           pad: int) -> list[dict]:
        """
        Organize the presets buttons in the frame.
        :param buttons: The list of buttons to organize.
        :param width: The width of the frame.
        :param pad: The padding between the buttons.

        :return: The organized buttons.
        """
        rows, row = [], []
        row_width, row_height = 0, 0
        for button in buttons:
            button_width = button.winfo_width() + (pad if row_width > 0 else 0)
            if row_width + button_width < width and len(row) < 8:
                row.append(button)
                row_width += button_width
                row_height = max(row_height, button.winfo_height())
            else:
                rows.append({'buttons': row,
                             'width': row_width,
                             'height': row_height,
                             'pad': (width - row_width) / 2})
                row = [button]
                row_width = button.winfo_width()
                row_height = button.winfo_height()
        rows.append({'buttons': row,
                     'width': row_width,
                     'height': row_height,
                     'pad': (width - row_width) / 2})
        return rows

    def __make_bottom_frame(self, image: ctk.CTkImage) -> dict[str, Any]:
        """
        Create the bottom frame of the window.
        :param image: The image of the preview button.

        :return: The layout of the bottom frame.
        """
        bottom_frame = ctk.CTkFrame(self)
        bottom_frame.pack(fill='x', padx=5, pady=6)
        gui = {'preview': ctk.CTkButton(bottom_frame, text='', hover=False,
                                        image=image, fg_color='transparent')}
        gui['preview'].grid(row=0, column=0, rowspan=3, pady=5)

        l1 = ctk.CTkLabel(bottom_frame, text=' Prossime trasmissioni:',
                          font=self.__font, anchor="w")
        l1.grid(row=0, column=1, columnspan=2, sticky='we', pady=(5, 2))

        style = ttk.Style()
        style.theme_use("default")
        style.configure("Treeview",
                        background="gray99",
                        foreground="#444",
                        rowheight=ceil(20 * self.scale),
                        fieldbackground="gray95",
                        borderwidth=0,
                        font=('Arial', ceil(10 * self.scale)))
        style.map('Treeview', background=[('selected', '#22559b')])
        style.configure("Treeview.Heading",
                        background="#3B8ED0",
                        foreground="gray99",
                        font=('Arial', ceil(9 * self.scale), 'bold'),
                        padding=ceil(4 * self.scale))
        style.map("Treeview.Heading",
                  background=[('active', '#36719F')])

        events = ttk.Treeview(bottom_frame, show="headings", height=6)
        sb = ctk.CTkScrollbar(bottom_frame, orientation="vertical",
                              command=events.yview)
        events.configure(yscrollcommand=sb.set)
        gui['events'] = events
        gui['events_scrollbar'] = sb
        events['columns'] = ('#', 'Tipo', 'Giorno', 'Ora')
        for i, col in enumerate(events['columns']):
            events.column(col, anchor='center',
                          width=0 if i <= 1 else ceil(100 * self.scale))
            events.heading(col, text=col, anchor='center')
        events.tag_configure('odd', background='#d4ecff')
        events.grid(row=1, column=1, columnspan=2, sticky='nsew')

        def deselect():
            if len(events.selection()) > 0:
                events.selection_remove(events.selection()[0])
        events.bind("<Button-1>", lambda e: deselect())

        icon = TablerIcons.load(OutlineIcon.CALENDAR_EVENT, size=100)
        gui['prog_button'] = ctk.CTkButton(bottom_frame, text='Programmazione',
                                           width=90, font=self.__b_font,
                                           image=CTkImage(icon))
        gui['prog_button'].grid(row=2, column=1, sticky='e', padx=5, pady=15)
        icon = TablerIcons.load(OutlineIcon.LOGS, size=100)
        gui['log_button'] = ctk.CTkButton(bottom_frame, text='Logs',
                                          width=90, font=self.__b_font,
                                          image=CTkImage(icon))
        gui['log_button'].grid(row=2, column=2, sticky='w', padx=5, pady=15)

        bottom_frame.update()
        w = bottom_frame.winfo_width() - int(l1.winfo_geometry().split('+')[1])
        l1.configure(width=w / self.scale - 7)
        sb.configure(height=events.winfo_height() // self.scale)

        return gui

    def __make_status_bar(self) -> dict[str, Any]:
        """
        Create the status bar of the window.

        :return: The layout of the status bar.
        """
        f1 = ctk.CTkFrame(self, corner_radius=0, fg_color='gray70',
                          border_width=1, border_color='gray84')
        f1.pack(fill='x', anchor='s', expand=True)
        f2 = ctk.CTkFrame(f1, corner_radius=0, fg_color='gray97')
        f2.pack(fill='x', anchor='s', expand=True, pady=(2, 0))
        l1 = ctk.CTkLabel(f2, text='', fg_color='gray97', anchor='w',
                          font=self.__font)
        l1.pack(fill='x', expand=True, padx=6)
        return {'status_bar': l1}

    def set_live_status(self, status: str) -> None:
        """
        Set the status of the live.
        :param status: The status of the live.
        """
        if status not in self.LIVE_IMAGES:
            return
        self.__gui['live_button'].configure(image=self.__images[status])

    def set_live_button_callback(self, callback: Callable) -> None:
        """
        Set the callback of the live button.
        :param callback: The callback of the live button.
        """
        self.__gui['live_button'].configure(command=callback)

    def get_title_description(self) -> tuple[str, str]:
        """
        Get the title and the description of the live.

        :return: The title and the description of the live.
        """
        return (self.__gui['title'][0].get().strip(),
                self.__gui['description'].get("1.0", "end-1c").strip())

    def set_title_description(self, title: str, description: str) -> None:
        """
        Set the title and the description of the live.
        :param title: The title of the live.
        :param description: The description of the live.
        """
        self.__gui['title'][0].set(title.strip())
        self.__gui['description'].delete("1.0", "end")
        self.__gui['description'].insert("1.0", description.strip())

    def set_enabled_title_description(self, enabled: bool) -> None:
        """
        Set the enabled status of title and description.
        :param enabled: The enabled status.
        """
        state = 'normal' if enabled else 'disabled'
        title = self.__gui['title'][1]

        fg_color = self.__gui['title'][2] if enabled else 'gray95'
        color = self.__gui['title'][3] if enabled else 'gray30'
        title.configure(state=state, text_color=color, fg_color=fg_color)
        self.__gui['description'].configure(state=state, text_color=color,
                                            fg_color=fg_color)

    def remaining_time(self, seconds: int) -> None:
        """
        Update the remaining time label.
        :param seconds: The remaining time in seconds.
        """
        if seconds <= 0:
            self.__gui['end_time'].configure(text='00m 00s')
            return
        minutes, seconds = divmod(seconds, 60)
        self.__gui['end_time'].configure(text=f'{minutes:02d}m {seconds:02d}s')

    def set_time_button_callback(self, callback: Callable) -> None:
        """
        Set the callback of the time button.
        :param callback: The callback of the time button.
        """
        self.__gui['time_button'].configure(command=callback)

    def set_presets_callback(self, callback: Callable[[str], None]) -> None:
        """
        Set the callback of the presets buttons.
        :param callback: The callback of the presets buttons.
        """
        self.__preset_callback_function = callback

    def set_prog_view_callback(self, callback: Callable) -> None:
        """
        Set the callback of the programming view button.
        :param callback: The callback of the programming view button.
        """
        def cmd(event):
            event_id = self.__gui['events'].identify_row(event.y)
            if event_id != '':
                callback(int(event_id))
        self.__gui['events'].bind("<Double-1>", cmd)

    def get_preview_size(self) -> tuple[int, int]:
        """
        Get the size of the preview image.

        :return: The size of the preview image.
        """
        return self.__images['no_camera'].cget('size')

    def set_preview_button_callback(self, callback: Callable) -> None:
        """
        Set the callback of the preview button.
        :param callback: The callback of the preview button.
        """
        self.__gui['preview'].configure(command=callback)

    def set_preview_image(self, image: Image = None) -> None:
        """
        Set the image of the preview button.
        :param image: The image to set. If None, set the no camera image.
        """
        if image:
            border = 1
            size = self.__images['no_camera'].cget('size')
            img = image.resize(tuple(x - 2 * border for x in size), 1)
            img = ImageOps.expand(img, border=border, fill="#999")
            img = ctk.CTkImage(img, size=size)
        else:
            img = self.__images['no_camera']
        self.__gui['preview'].configure(image=img)

    def set_events(self, events: list[tuple[str, str, str]]) -> None:
        """
        Set the events in the events treeview.
        :param events: The list of events to set.
        """
        self.__gui['events'].delete(*self.__gui['events'].get_children())
        for i, event in enumerate(events):
            self.__gui['events'].insert('', 'end', iid=i,
                                        values=(i + 1, *event),
                                        tags=('odd' if i % 2 else 'even'))
        self.__gui['events'].yview_moveto(0.0)
        if self.__gui['events'].yview() == (0.0, 1.0):
            self.__gui['events_scrollbar'].grid_forget()
        else:
            self.__gui['events_scrollbar'].grid(row=1, column=2, sticky='e')

    def set_prog_button_callback(self, callback: Callable) -> None:
        """
        Set the callback of the programming button.
        :param callback: The callback of the programming button.
        """
        self.__gui['prog_button'].configure(command=callback)

    def set_log_button_callback(self, callback: Callable) -> None:
        """
        Set the callback of the logs button.
        :param callback: The callback of the logs button.
        """
        self.__gui['log_button'].configure(command=callback)

    def set_status_message(self, status: str, level: str = 'INFO') -> None:
        """
        Set the status message in the status bar.
        :param status: The status message.
        :param level: The level of the log message.
        """
        if level in self.COLORS:
            self.__gui['status_bar'].configure(text=str(status),
                                               text_color=self.COLORS[level])

__init__(presets)

Initialize the main window. :param presets: The list of presets.

Source code in main_view.py
def __init__(self, presets: list[str]):
    """
    Initialize the main window.
    :param presets: The list of presets.
    """
    super().__init__()
    width, height = 750, 520
    self.geometry(f'{width}x{height}')
    self.title('Streaming Santa Croce')
    self.iconbitmap(self.ICON)
    self.resizable(False, False)

    self.__font = ('Arial', 14)
    self.__b_font = ('Arial', 15, 'bold')
    self.scale = self.winfo_width() / width
    self.__preset_callback_function: Callable[[str], None] = lambda x: None
    self.__images, self.__gui = self.__make_layout(presets)

__load_images() classmethod

Load the images used in the window.

:return: The images' dictionary.

Source code in main_view.py
@classmethod
def __load_images(cls) -> dict[str, ctk.CTkImage]:
    """
    Load the images used in the window.

    :return: The images' dictionary.
    """
    images = {
        'ready': Image.open(cls.LIVE_IMAGES['ready']),
        'prepared': Image.open(cls.LIVE_IMAGES['prepared']),
        'live': Image.open(cls.LIVE_IMAGES['live']),
        'no_camera': Image.open(cls.NO_CAMERA)
    }
    scale = 0.95
    for img in ('ready', 'prepared', 'live'):
        images[img] = images[img].resize(
            (int(images[img].size[0] * scale),
             int(images[img].size[1] * scale)), 1)
    scale = 0.17
    images['no_camera'] = images['no_camera'].resize(
        (int(images['no_camera'].size[0] * scale),
         int(images['no_camera'].size[1] * scale * 1.1)), 1)
    for img in images:
        images[img] = ctk.CTkImage(images[img], size=images[img].size)
    return images

__make_bottom_frame(image)

Create the bottom frame of the window. :param image: The image of the preview button.

:return: The layout of the bottom frame.

Source code in main_view.py
def __make_bottom_frame(self, image: ctk.CTkImage) -> dict[str, Any]:
    """
    Create the bottom frame of the window.
    :param image: The image of the preview button.

    :return: The layout of the bottom frame.
    """
    bottom_frame = ctk.CTkFrame(self)
    bottom_frame.pack(fill='x', padx=5, pady=6)
    gui = {'preview': ctk.CTkButton(bottom_frame, text='', hover=False,
                                    image=image, fg_color='transparent')}
    gui['preview'].grid(row=0, column=0, rowspan=3, pady=5)

    l1 = ctk.CTkLabel(bottom_frame, text=' Prossime trasmissioni:',
                      font=self.__font, anchor="w")
    l1.grid(row=0, column=1, columnspan=2, sticky='we', pady=(5, 2))

    style = ttk.Style()
    style.theme_use("default")
    style.configure("Treeview",
                    background="gray99",
                    foreground="#444",
                    rowheight=ceil(20 * self.scale),
                    fieldbackground="gray95",
                    borderwidth=0,
                    font=('Arial', ceil(10 * self.scale)))
    style.map('Treeview', background=[('selected', '#22559b')])
    style.configure("Treeview.Heading",
                    background="#3B8ED0",
                    foreground="gray99",
                    font=('Arial', ceil(9 * self.scale), 'bold'),
                    padding=ceil(4 * self.scale))
    style.map("Treeview.Heading",
              background=[('active', '#36719F')])

    events = ttk.Treeview(bottom_frame, show="headings", height=6)
    sb = ctk.CTkScrollbar(bottom_frame, orientation="vertical",
                          command=events.yview)
    events.configure(yscrollcommand=sb.set)
    gui['events'] = events
    gui['events_scrollbar'] = sb
    events['columns'] = ('#', 'Tipo', 'Giorno', 'Ora')
    for i, col in enumerate(events['columns']):
        events.column(col, anchor='center',
                      width=0 if i <= 1 else ceil(100 * self.scale))
        events.heading(col, text=col, anchor='center')
    events.tag_configure('odd', background='#d4ecff')
    events.grid(row=1, column=1, columnspan=2, sticky='nsew')

    def deselect():
        if len(events.selection()) > 0:
            events.selection_remove(events.selection()[0])
    events.bind("<Button-1>", lambda e: deselect())

    icon = TablerIcons.load(OutlineIcon.CALENDAR_EVENT, size=100)
    gui['prog_button'] = ctk.CTkButton(bottom_frame, text='Programmazione',
                                       width=90, font=self.__b_font,
                                       image=CTkImage(icon))
    gui['prog_button'].grid(row=2, column=1, sticky='e', padx=5, pady=15)
    icon = TablerIcons.load(OutlineIcon.LOGS, size=100)
    gui['log_button'] = ctk.CTkButton(bottom_frame, text='Logs',
                                      width=90, font=self.__b_font,
                                      image=CTkImage(icon))
    gui['log_button'].grid(row=2, column=2, sticky='w', padx=5, pady=15)

    bottom_frame.update()
    w = bottom_frame.winfo_width() - int(l1.winfo_geometry().split('+')[1])
    l1.configure(width=w / self.scale - 7)
    sb.configure(height=events.winfo_height() // self.scale)

    return gui

__make_layout(presets)

Create the layout of the main window. :param presets: The list of presets.

:return: The images dictionary and the layout.

Source code in main_view.py
def __make_layout(self, presets: list[str]) \
        -> tuple[dict[str, ctk.CTkImage], dict[str, Any]]:
    """
    Create the layout of the main window.
    :param presets: The list of presets.

    :return: The images dictionary and the layout.
    """
    images = self.__load_images()
    gui = self.__make_live_frame(images['ready'])
    gui['presets'] = self.__make_presets_frame(presets)
    gui.update(self.__make_bottom_frame(images['no_camera']))
    gui.update(self.__make_status_bar())
    return images, gui

__make_live_frame(image)

Create the top frame that contains the live settings. :param image: The live image for the button.

:return: The layout of the top frame.

Source code in main_view.py
def __make_live_frame(self, image: ctk.CTkImage) -> dict[str, Any]:
    """
    Create the top frame that contains the live settings.
    :param image: The live image for the button.

    :return: The layout of the top frame.
    """
    live_frame = ctk.CTkFrame(self)
    live_frame.pack(fill='x', padx=5, pady=6)

    b1 = ctk.CTkButton(live_frame, image=image, text='', hover=False,
                       fg_color='transparent')
    b1.grid(row=0, column=0, rowspan=3, padx=(5, 0))
    gui = {'live_button': b1}

    l1 = ctk.CTkLabel(live_frame, text='Titolo:', font=self.__font)
    l1.grid(row=0, column=1, sticky='w', pady=5)
    l2 = ctk.CTkLabel(live_frame, text='Descrizione:', font=self.__font)
    l2.grid(row=1, column=1, rowspan=2, sticky='w',
            pady=(0, 5), padx=(0, 10))

    txt_var = ctk.StringVar()
    e1 = ctk.CTkEntry(live_frame, font=self.__font, border_width=1,
                      textvariable=txt_var)
    e1.grid(row=0, column=2, sticky="w", pady=5)
    gui['title'] = (txt_var, e1, e1.cget('fg_color'), e1.cget('text_color'))
    e2 = ctk.CTkTextbox(live_frame, font=self.__font, border_width=1,
                        height=70, wrap='word')
    e2.grid(row=1, column=2, rowspan=2, sticky="ns", pady=(0, 5))
    gui['description'] = e2

    l3 = ctk.CTkLabel(live_frame, text='Al termine', font=self.__font)
    l3.grid(row=0, column=3)
    l4 = ctk.CTkLabel(live_frame, text='00m 00s', font=('Arial', 25),
                      width=120)
    l4.grid(row=1, column=3, sticky='n', padx=10)
    gui['end_time'] = l4
    gui['time_button'] = ctk.CTkButton(live_frame, text='+ 10min', width=90,
                                       font=self.__b_font)
    gui['time_button'].grid(row=2, column=3, sticky='n')

    live_frame.update()
    lw = (live_frame.winfo_width() - int(e1.winfo_geometry().split('+')[1])
          - l4.winfo_width()) / self.scale - 20
    e1.configure(width=lw)
    e2.configure(width=lw)
    return gui

__make_presets_frame(presets)

Create the frame that contains the presets buttons. :param presets: The list of presets.

:return: The layout of the presets' frame.

Source code in main_view.py
def __make_presets_frame(self, presets: list) -> list[ctk.CTkButton]:
    """
    Create the frame that contains the presets buttons.
    :param presets: The list of presets.

    :return: The layout of the presets' frame.
    """
    preset_frame = ctk.CTkFrame(self)
    preset_frame.pack(fill='x', padx=5)
    preset_frame.update()
    frame_width = preset_frame.winfo_width()

    class PresetButton(ctk.CTkButton):  # pylint: disable=too-many-ancestors
        """
        A preset button.
        """

        def __init__(self, master, **kw):
            super().__init__(master, **kw)
            self._text_label.configure(wraplength=frame_width // 8)

    buttons = []
    for preset in presets:
        button = PresetButton(preset_frame, text=preset, width=80,
                              height=45, font=self.__b_font)
        button.configure(command=lambda p=preset: self.__preset_callback(p))
        button.place(x=0, y=0)
        buttons.append(button)
    preset_frame.update()
    next_y = 10
    pad_x = 5
    for row in self.__organize_presets(buttons, frame_width,
                                       ceil(5 * self.scale)):
        next_x = row['pad'] / self.scale
        for button in row['buttons']:
            button.place(x=next_x, y=next_y)
            next_x += button.winfo_width() / self.scale + pad_x
        next_y += row['height'] / self.scale + 10
    preset_frame.configure(height=next_y)
    return buttons

__make_status_bar()

Create the status bar of the window.

:return: The layout of the status bar.

Source code in main_view.py
def __make_status_bar(self) -> dict[str, Any]:
    """
    Create the status bar of the window.

    :return: The layout of the status bar.
    """
    f1 = ctk.CTkFrame(self, corner_radius=0, fg_color='gray70',
                      border_width=1, border_color='gray84')
    f1.pack(fill='x', anchor='s', expand=True)
    f2 = ctk.CTkFrame(f1, corner_radius=0, fg_color='gray97')
    f2.pack(fill='x', anchor='s', expand=True, pady=(2, 0))
    l1 = ctk.CTkLabel(f2, text='', fg_color='gray97', anchor='w',
                      font=self.__font)
    l1.pack(fill='x', expand=True, padx=6)
    return {'status_bar': l1}

__organize_presets(buttons, width, pad) staticmethod

Organize the presets buttons in the frame. :param buttons: The list of buttons to organize. :param width: The width of the frame. :param pad: The padding between the buttons.

:return: The organized buttons.

Source code in main_view.py
@staticmethod
def __organize_presets(buttons: list[ctk.CTkButton], width: int,
                       pad: int) -> list[dict]:
    """
    Organize the presets buttons in the frame.
    :param buttons: The list of buttons to organize.
    :param width: The width of the frame.
    :param pad: The padding between the buttons.

    :return: The organized buttons.
    """
    rows, row = [], []
    row_width, row_height = 0, 0
    for button in buttons:
        button_width = button.winfo_width() + (pad if row_width > 0 else 0)
        if row_width + button_width < width and len(row) < 8:
            row.append(button)
            row_width += button_width
            row_height = max(row_height, button.winfo_height())
        else:
            rows.append({'buttons': row,
                         'width': row_width,
                         'height': row_height,
                         'pad': (width - row_width) / 2})
            row = [button]
            row_width = button.winfo_width()
            row_height = button.winfo_height()
    rows.append({'buttons': row,
                 'width': row_width,
                 'height': row_height,
                 'pad': (width - row_width) / 2})
    return rows

__preset_callback(preset)

Call the given preset on the camera. :param preset: The preset name to call.

Source code in main_view.py
def __preset_callback(self, preset: str) -> None:
    """
    Call the given preset on the camera.
    :param preset: The preset name to call.
    """
    try:
        self.__preset_callback_function(preset)
    except AttributeError:
        pass

get_preview_size()

Get the size of the preview image.

:return: The size of the preview image.

Source code in main_view.py
def get_preview_size(self) -> tuple[int, int]:
    """
    Get the size of the preview image.

    :return: The size of the preview image.
    """
    return self.__images['no_camera'].cget('size')

get_title_description()

Get the title and the description of the live.

:return: The title and the description of the live.

Source code in main_view.py
def get_title_description(self) -> tuple[str, str]:
    """
    Get the title and the description of the live.

    :return: The title and the description of the live.
    """
    return (self.__gui['title'][0].get().strip(),
            self.__gui['description'].get("1.0", "end-1c").strip())

remaining_time(seconds)

Update the remaining time label. :param seconds: The remaining time in seconds.

Source code in main_view.py
def remaining_time(self, seconds: int) -> None:
    """
    Update the remaining time label.
    :param seconds: The remaining time in seconds.
    """
    if seconds <= 0:
        self.__gui['end_time'].configure(text='00m 00s')
        return
    minutes, seconds = divmod(seconds, 60)
    self.__gui['end_time'].configure(text=f'{minutes:02d}m {seconds:02d}s')

set_enabled_title_description(enabled)

Set the enabled status of title and description. :param enabled: The enabled status.

Source code in main_view.py
def set_enabled_title_description(self, enabled: bool) -> None:
    """
    Set the enabled status of title and description.
    :param enabled: The enabled status.
    """
    state = 'normal' if enabled else 'disabled'
    title = self.__gui['title'][1]

    fg_color = self.__gui['title'][2] if enabled else 'gray95'
    color = self.__gui['title'][3] if enabled else 'gray30'
    title.configure(state=state, text_color=color, fg_color=fg_color)
    self.__gui['description'].configure(state=state, text_color=color,
                                        fg_color=fg_color)

set_events(events)

Set the events in the events treeview. :param events: The list of events to set.

Source code in main_view.py
def set_events(self, events: list[tuple[str, str, str]]) -> None:
    """
    Set the events in the events treeview.
    :param events: The list of events to set.
    """
    self.__gui['events'].delete(*self.__gui['events'].get_children())
    for i, event in enumerate(events):
        self.__gui['events'].insert('', 'end', iid=i,
                                    values=(i + 1, *event),
                                    tags=('odd' if i % 2 else 'even'))
    self.__gui['events'].yview_moveto(0.0)
    if self.__gui['events'].yview() == (0.0, 1.0):
        self.__gui['events_scrollbar'].grid_forget()
    else:
        self.__gui['events_scrollbar'].grid(row=1, column=2, sticky='e')

set_live_button_callback(callback)

Set the callback of the live button. :param callback: The callback of the live button.

Source code in main_view.py
def set_live_button_callback(self, callback: Callable) -> None:
    """
    Set the callback of the live button.
    :param callback: The callback of the live button.
    """
    self.__gui['live_button'].configure(command=callback)

set_live_status(status)

Set the status of the live. :param status: The status of the live.

Source code in main_view.py
def set_live_status(self, status: str) -> None:
    """
    Set the status of the live.
    :param status: The status of the live.
    """
    if status not in self.LIVE_IMAGES:
        return
    self.__gui['live_button'].configure(image=self.__images[status])

set_log_button_callback(callback)

Set the callback of the logs button. :param callback: The callback of the logs button.

Source code in main_view.py
def set_log_button_callback(self, callback: Callable) -> None:
    """
    Set the callback of the logs button.
    :param callback: The callback of the logs button.
    """
    self.__gui['log_button'].configure(command=callback)

set_presets_callback(callback)

Set the callback of the presets buttons. :param callback: The callback of the presets buttons.

Source code in main_view.py
def set_presets_callback(self, callback: Callable[[str], None]) -> None:
    """
    Set the callback of the presets buttons.
    :param callback: The callback of the presets buttons.
    """
    self.__preset_callback_function = callback

set_preview_button_callback(callback)

Set the callback of the preview button. :param callback: The callback of the preview button.

Source code in main_view.py
def set_preview_button_callback(self, callback: Callable) -> None:
    """
    Set the callback of the preview button.
    :param callback: The callback of the preview button.
    """
    self.__gui['preview'].configure(command=callback)

set_preview_image(image=None)

Set the image of the preview button. :param image: The image to set. If None, set the no camera image.

Source code in main_view.py
def set_preview_image(self, image: Image = None) -> None:
    """
    Set the image of the preview button.
    :param image: The image to set. If None, set the no camera image.
    """
    if image:
        border = 1
        size = self.__images['no_camera'].cget('size')
        img = image.resize(tuple(x - 2 * border for x in size), 1)
        img = ImageOps.expand(img, border=border, fill="#999")
        img = ctk.CTkImage(img, size=size)
    else:
        img = self.__images['no_camera']
    self.__gui['preview'].configure(image=img)

set_prog_button_callback(callback)

Set the callback of the programming button. :param callback: The callback of the programming button.

Source code in main_view.py
def set_prog_button_callback(self, callback: Callable) -> None:
    """
    Set the callback of the programming button.
    :param callback: The callback of the programming button.
    """
    self.__gui['prog_button'].configure(command=callback)

set_prog_view_callback(callback)

Set the callback of the programming view button. :param callback: The callback of the programming view button.

Source code in main_view.py
def set_prog_view_callback(self, callback: Callable) -> None:
    """
    Set the callback of the programming view button.
    :param callback: The callback of the programming view button.
    """
    def cmd(event):
        event_id = self.__gui['events'].identify_row(event.y)
        if event_id != '':
            callback(int(event_id))
    self.__gui['events'].bind("<Double-1>", cmd)

set_status_message(status, level='INFO')

Set the status message in the status bar. :param status: The status message. :param level: The level of the log message.

Source code in main_view.py
def set_status_message(self, status: str, level: str = 'INFO') -> None:
    """
    Set the status message in the status bar.
    :param status: The status message.
    :param level: The level of the log message.
    """
    if level in self.COLORS:
        self.__gui['status_bar'].configure(text=str(status),
                                           text_color=self.COLORS[level])

set_time_button_callback(callback)

Set the callback of the time button. :param callback: The callback of the time button.

Source code in main_view.py
def set_time_button_callback(self, callback: Callable) -> None:
    """
    Set the callback of the time button.
    :param callback: The callback of the time button.
    """
    self.__gui['time_button'].configure(command=callback)

set_title_description(title, description)

Set the title and the description of the live. :param title: The title of the live. :param description: The description of the live.

Source code in main_view.py
def set_title_description(self, title: str, description: str) -> None:
    """
    Set the title and the description of the live.
    :param title: The title of the live.
    :param description: The description of the live.
    """
    self.__gui['title'][0].set(title.strip())
    self.__gui['description'].delete("1.0", "end")
    self.__gui['description'].insert("1.0", description.strip())