|
1 | 1 | import functools |
2 | 2 | import tkinter as tk |
3 | | -from tkinter import ttk |
4 | | -from PIL import Image, ImageTk |
| 3 | + |
| 4 | +from typing import List, Dict, Union |
| 5 | +from PIL import Image, ImageEnhance, ImageTk |
5 | 6 | from support.Device import devices, Device |
6 | 7 |
|
7 | | -def select_input(device: Device): |
8 | | - device.select() |
| 8 | +class Colors: |
| 9 | + FRAME = "Black" |
| 10 | + BUTTON_NORMAL = "Dark Grey" |
| 11 | + BUTTON_SELECTED = "White" |
9 | 12 |
|
10 | 13 | class Main(tk.Tk): |
11 | 14 |
|
12 | 15 | def __init__(self): |
13 | 16 | super().__init__() |
| 17 | + self.after_idle(self.__idle_poll) |
14 | 18 | self.title('Pi Game Switch') |
15 | 19 | self.attributes('-fullscreen', True) |
16 | | - |
17 | | - # Get the number of columns and rows we will have. |
18 | | - columns = int(self.winfo_screenwidth() / 128) - 1 |
19 | | - rows = int(self.winfo_screenheight() / 128) - 1 |
| 20 | + self.__normal_images = {} # type: Dict[tk.Button, tk.PhotoImage] |
| 21 | + self.__selected_images = {} # type: Dict[tk.Button, tk.PhotoImage] |
| 22 | + self.__buttons = [] # type: List[tk.Button] |
| 23 | + self.__frame = tk.Frame(self, cursor='none', background=Colors.FRAME) |
| 24 | + self.__selected = None # type: Union[None, tk.Button] |
20 | 25 |
|
21 | 26 | # Configure the layout |
22 | | - self.__frame = ttk.Frame(self) |
23 | | - self.__frame.grid(columns=columns, rows=rows, sticky=(tk.N, tk.W, tk.E, tk.S)) |
| 27 | + self.__frame.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E, tk.S)) |
24 | 28 | self.columnconfigure(0, weight=1) |
25 | 29 | self.rowconfigure(0, weight=1) |
26 | 30 |
|
27 | | - self.__buttons = [] |
28 | | - self.__images = [] |
| 31 | + # Get the number of columns we will have. |
| 32 | + columns = int(self.winfo_screenwidth() / 130) |
29 | 33 |
|
30 | 34 | column = 0 |
31 | 35 | row = 0 |
32 | 36 | for device in devices: |
33 | | - command = functools.partial(select_input, device) |
| 37 | + # Create the button. |
| 38 | + # noinspection SpellCheckingInspection |
| 39 | + button_config = { |
| 40 | + 'borderwidth': 0, |
| 41 | + 'highlightthickness': 0, |
| 42 | + 'activebackground': Colors.BUTTON_NORMAL, |
| 43 | + 'background': Colors.BUTTON_NORMAL |
| 44 | + } |
34 | 45 | if len(device.image) > 0: |
35 | | - image = ImageTk.PhotoImage(Image.open(device.image)) |
36 | | - self.__images.append(image) |
37 | | - button = ttk.Button(self.__frame, image=image, command=command) |
38 | | - button.grid(column=column, row=row, sticky=(tk.N, tk.W)) |
39 | | - self.__buttons.append(button) |
| 46 | + # Get the selected image. |
| 47 | + selected = ImageTk.PhotoImage(Image.open(device.image)) |
| 48 | + # Generate the normal image. |
| 49 | + enhancer = ImageEnhance.Brightness(Image.open(device.image)) |
| 50 | + normal = ImageTk.PhotoImage(enhancer.enhance(0.66)) |
| 51 | + |
| 52 | + button = tk.Button(self.__frame, image=normal, **button_config) |
| 53 | + self.__selected_images[button] = selected |
| 54 | + self.__normal_images[button] = normal |
40 | 55 | else: |
41 | 56 | text = device.title |
42 | | - button = ttk.Button(self.__frame, text=text, command=command) |
43 | | - button.grid(column=column, row=row, sticky=(tk.N, tk.W)) |
44 | | - self.__buttons.append(button) |
| 57 | + button = tk.Button(self.__frame, text=text, **button_config) |
| 58 | + |
| 59 | + # Generate a partial to bind the command to the device. |
| 60 | + command = functools.partial(self.__select_device, button, device) |
45 | 61 |
|
| 62 | + # Add the new button. |
| 63 | + button.config(command=command) |
| 64 | + button.grid(column=column, row=row, sticky=(tk.N, tk.W)) |
| 65 | + button.grid_configure(padx=1, pady=1) |
| 66 | + self.__buttons.append(button) |
| 67 | + |
| 68 | + # Move the column and row positions as necessary. |
46 | 69 | column = column + 1 |
47 | 70 | if column == columns: |
48 | 71 | row = row + 1 |
49 | 72 | column = 0 |
| 73 | + |
| 74 | + def __select_device(self, button: tk.Button, device: Device): |
| 75 | + selected = self.__selected |
| 76 | + if selected: |
| 77 | + selected.config(activebackground=Colors.BUTTON_NORMAL, background=Colors.BUTTON_NORMAL) |
| 78 | + if selected in self.__normal_images: |
| 79 | + selected.config(image=self.__normal_images[selected]) |
| 80 | + |
| 81 | + device.select() |
| 82 | + self.__selected = button |
| 83 | + button.config(activebackground=Colors.BUTTON_SELECTED, background=Colors.BUTTON_SELECTED) |
| 84 | + if button in self.__selected_images: |
| 85 | + button.config(image=self.__selected_images[button]) |
| 86 | + |
| 87 | + def __idle_poll(self) -> None: |
| 88 | + self.after(500, self.__idle_poll) |
0 commit comments