|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import scrolledtext, Scrollbar |
| 3 | +import keyboard |
| 4 | +from time import sleep |
| 5 | + |
| 6 | +class Automat(tk.Tk): |
| 7 | + |
| 8 | + def get_entries_list(self) -> list[str]: |
| 9 | + entries_text = self.spp_area.get("1.0", tk.END) |
| 10 | + entries_list = list(filter(None, entries_text.split('\n'))) |
| 11 | + |
| 12 | + return entries_list |
| 13 | + |
| 14 | + def on_clear(self) -> None: |
| 15 | + self.spp_area.delete("1.0", tk.END) |
| 16 | + |
| 17 | + def on_exit(self) -> None: |
| 18 | + self.destroy() |
| 19 | + |
| 20 | + def highlight_line(self, line_number: int) -> None: |
| 21 | + self.spp_area.tag_remove("highlight", "1.0", tk.END) |
| 22 | + self.spp_area.tag_add("highlight", f"{line_number}.0", f"{line_number}.end") |
| 23 | + self.spp_area.tag_config("highlight", background="yellow") |
| 24 | + |
| 25 | + def return_to_first(self) -> None: |
| 26 | + self.highlight_counter = 1 |
| 27 | + self.highlight_line(self.highlight_counter) |
| 28 | + |
| 29 | + def step_back(self) -> None: |
| 30 | + self.highlight_counter -= 1 |
| 31 | + if self.highlight_counter < 1: |
| 32 | + self.highlight_counter = len(self.get_entries_list()) |
| 33 | + self.highlight_line(self.highlight_counter) |
| 34 | + |
| 35 | + def step_forward(self) -> None: |
| 36 | + self.highlight_counter += 1 |
| 37 | + if self.highlight_counter > len(self.get_entries_list()): |
| 38 | + self.highlight_counter = 1 |
| 39 | + self.highlight_line(self.highlight_counter) |
| 40 | + |
| 41 | + def listen_spp(self, event) -> None: |
| 42 | + self.spp_area.edit_modified(False) |
| 43 | + self.return_to_first() |
| 44 | + |
| 45 | + def prevent_newline(self, event) -> None: |
| 46 | + return "break" |
| 47 | + |
| 48 | + def paste_hotkey(self) -> None: |
| 49 | + id_text = self.id_area.get("1.0", tk.END) |
| 50 | + id_text = id_text.removesuffix('\n') |
| 51 | + if len(entries_list := self.get_entries_list()) >= self.highlight_counter: |
| 52 | + spp_text = entries_list[self.highlight_counter-1] |
| 53 | + else: |
| 54 | + spp_text = '' |
| 55 | + |
| 56 | + keyboard.write(id_text) |
| 57 | + sleep(.1) |
| 58 | + keyboard.press_and_release('tab') |
| 59 | + sleep(.1) |
| 60 | + keyboard.write(spp_text) |
| 61 | + |
| 62 | + self.highlight_counter += 1 |
| 63 | + if self.highlight_counter > len(entries_list): |
| 64 | + self.highlight_counter = 1 |
| 65 | + self.highlight_line(self.highlight_counter) |
| 66 | + |
| 67 | + def toggle(self): |
| 68 | + self.status = not self.status |
| 69 | + self.status_label.config(state="normal") |
| 70 | + self.status_label.delete('1.0', tk.END) |
| 71 | + if self.status: |
| 72 | + self.status_label.insert("1.0", f"Status: Aktywny ") |
| 73 | + self.status_label.tag_add("highlight", f"1.7", tk.END) |
| 74 | + self.status_label.tag_config("highlight", background='#06402b', foreground="white") |
| 75 | + keyboard.add_hotkey('f9', self.paste_hotkey) |
| 76 | + self.status_toggle.config(text='Wyłącz') |
| 77 | + self.status_explanation.config(text="Kliknij na miejsce, gdzie powinno się wpisać identyfikator i wciśnij F9") |
| 78 | + else: |
| 79 | + self.status_label.insert("1.0", f"Status: Nieaktywny") |
| 80 | + self.status_label.tag_add("highlight", f"1.7", tk.END) |
| 81 | + self.status_label.tag_config("highlight", background='#a62c2b', foreground="white") |
| 82 | + keyboard.remove_all_hotkeys() |
| 83 | + self.status_toggle.config(text='Włącz') |
| 84 | + self.status_explanation.config(text="") |
| 85 | + self.status_label.config(state="disabled") |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + def __init__(self) -> None: |
| 90 | + super().__init__() |
| 91 | + |
| 92 | + self.highlight_counter = 0 |
| 93 | + self.status = False |
| 94 | + |
| 95 | + frame = tk.Frame(self) |
| 96 | + frame.pack(padx=10, pady=10) |
| 97 | + |
| 98 | + ## Segment identyfikatora |
| 99 | + id_frame = tk.Frame(frame) |
| 100 | + id_frame.grid(row=1, column=0, padx=5, pady=5, sticky='nw') |
| 101 | + |
| 102 | + self.id_label = tk.Label(id_frame, text="Numer identyfikatora: ") |
| 103 | + self.id_label.pack(fill=tk.X, pady=2, anchor='w') |
| 104 | + |
| 105 | + self.id_area = tk.Text(id_frame, width=15, height=1) |
| 106 | + self.id_area.pack(fill=tk.X, pady=2, anchor='w') |
| 107 | + self.id_area.bind("<Return>", self.prevent_newline) |
| 108 | + |
| 109 | + ## Segment włączania |
| 110 | + toggle_frame = tk.Frame(frame) |
| 111 | + toggle_frame.grid(row=1, column=1, padx=5, pady=5) |
| 112 | + |
| 113 | + self.status_label = tk.Text(toggle_frame, width=19, height=1, background="#f0f0f0") |
| 114 | + self.status_label.insert("1.0", f"Status: Nieaktywny") |
| 115 | + self.status_label.tag_add("highlight", f"1.7", tk.END) |
| 116 | + self.status_label.tag_config("highlight", background='#a62c2b', foreground="white") |
| 117 | + self.status_label.config(state="disabled") |
| 118 | + self.status_label.pack(fill=tk.X, pady=2) |
| 119 | + |
| 120 | + self.status_toggle = tk.Button(toggle_frame, text="Włącz", width=10, height=1, command=self.toggle) |
| 121 | + self.status_toggle.pack(fill=tk.X, pady=2) |
| 122 | + |
| 123 | + |
| 124 | + ## Segment indeksów |
| 125 | + self.spp_label = tk.Label(frame, text="Indeksy SPP: ") |
| 126 | + self.spp_label.grid(row=2, column=0, sticky="w") |
| 127 | + |
| 128 | + self.spp_area = scrolledtext.ScrolledText(frame, width=20, height=16, wrap=tk.WORD) |
| 129 | + self.spp_area.grid(row=3, column=0, padx=5, pady=5) |
| 130 | + self.spp_area.bind('<<Modified>>', self.listen_spp) |
| 131 | + |
| 132 | + ## Segment przycisków |
| 133 | + button_frame = tk.Frame(frame) |
| 134 | + button_frame.grid(row=3, column=1, padx=5, pady=5, sticky="ns") |
| 135 | + |
| 136 | + self.status_explanation = tk.Label(button_frame, text='', wraplength=160, height=4, font=13, width=20) |
| 137 | + self.status_explanation.pack(fill=tk.X, pady=(0, 40), anchor='n') |
| 138 | + |
| 139 | + clear_btn = tk.Button(button_frame, text="Wyczyść", command=self.on_clear) |
| 140 | + clear_btn.pack(fill=tk.X, pady=2) |
| 141 | + |
| 142 | + to_first_btn = tk.Button(button_frame, text="⧋ Wróć do pierwszego", command=self.return_to_first) |
| 143 | + to_first_btn.pack(fill=tk.X, pady=2) |
| 144 | + |
| 145 | + return_btn = tk.Button(button_frame, text="△ Poprzedni", command=self.step_back) |
| 146 | + return_btn.pack(fill=tk.X, pady=2) |
| 147 | + |
| 148 | + advance_btn = tk.Button(button_frame, text="▽ Następny", command=self.step_forward) |
| 149 | + advance_btn.pack(fill=tk.X, pady=2) |
| 150 | + |
| 151 | + exit_btn = tk.Button(button_frame, text="Wyjdź", command=self.on_exit) |
| 152 | + exit_btn.pack(fill=tk.X, pady=2) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == '__main__': |
| 156 | + root = Automat() |
| 157 | + root.title("Automat do otwierania gniazd") |
| 158 | + root.mainloop() |
0 commit comments