| 1 |  |  | import platform | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | import tkinter as tk | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | from tkinter import ttk | 
            
                                                        
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 6 |  |  | class ModalWindow(tk.Tk): | 
            
                                                        
            
                                    
            
            
                | 7 |  |  |     def __init__(self, title: str): | 
            
                                                        
            
                                    
            
            
                | 8 |  |  |         super().__init__() | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |         self.title(title) | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |         self.configure_window() | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |     def configure_window(self): | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |         if platform.system().lower() == 'windows': | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |             self.wm_attributes('-toolwindow', 'True') | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |         self.wm_attributes('-topmost', 'True') | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |         self.resizable(False, False) | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 19 |  |  | class RadiobuttonChooser(ModalWindow): | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |     Usage Example: | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |     ```python | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |     radiobutton_chooser = RadiobuttonChooser("Choose an option", {"Option 1": 1, "Option 2": 2}) | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |     choice, value = radiobutton_chooser.get_choice() | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |     print(choice, value) | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |     ``` | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |     def __init__(self, window_title: str, options: dict, default=None): | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         super().__init__(window_title) | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         self.exit_button = None | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         self.choice_var = tk.StringVar(self, None) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |         self.default = default | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         self.options = options | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |         self.create_widgets() | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |         self.mainloop() | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |     def create_widgets(self): | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |         frame = ttk.Frame(self, borderwidth=5, relief='solid') | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |         frame.pack(padx=4, pady=4) | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         ttk.Label(frame, text="Output Mode:").pack() | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         for option in self.options: | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |             ttk.Radiobutton( | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |                 frame, | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |                 text=option, | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |                 variable=self.choice_var, | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |                 value=option, | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |                 command=self.enable_exit_button, | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                 padding=4 | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |             ).pack(side=tk.TOP, anchor="w") | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         self.exit_button = ttk.Button(self, text="OK", command=self.destroy, state=tk.DISABLED) | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         self.exit_button.pack(pady=4, side=tk.BOTTOM) | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     def enable_exit_button(self): | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         self.exit_button['state'] = tk.NORMAL | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     def get_choice(self): | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         return self.choice_var.get(), self.options.get(self.choice_var.get(), self.default) | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  | class ListboxChooser(ModalWindow): | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |     Usage Example: | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     ```python | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |     listbox_chooser = ListboxChooser("Choose options", {"Option 1": 1, "Option 2": 2, "Option 3": 3}) | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     for choice, value in listbox_chooser.get_choices(): | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     print(choice, value) | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     ``` | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |     def __init__(self, window_title: str, options: dict, default=None): | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         super().__init__(window_title) | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         self.exit_button = None | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         self.choices = list(options.keys()) | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         self.options = options | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |         self.default = default | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         self.selection_var = tk.Variable(self) | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |         self.create_widgets() | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         self.mainloop() | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |     def create_widgets(self): | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         frame = ttk.Frame(self) | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         frame.pack(padx=2, pady=2) | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |         listbox = tk.Listbox(frame, listvariable=tk.Variable(self, value=self.choices), selectmode="multiple", | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |                              height=min(len(self.choices), 50), width=max(len(c) for c in self.choices)) | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         listbox.pack(side=tk.LEFT, fill=tk.BOTH) | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         scrollbar = ttk.Scrollbar(frame, command=listbox.yview) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH) | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         listbox.config(yscrollcommand=scrollbar.set) | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         listbox.bind("<<ListboxSelect>>", self.on_select) | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         self.exit_button = ttk.Button(self, text="OK", command=self.destroy, state=tk.DISABLED) | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         self.exit_button.pack(pady=4, side=tk.BOTTOM) | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |     def on_select(self, event): | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         self.exit_button['state'] = tk.NORMAL | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         self.selection_var.set(event.widget.curselection()) | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |     def get_choices(self): | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |         for choice in self.selection_var.get(): | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             yield self.choices[choice], self.options.get(self.choices[choice], self.default) | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |