| Conditions | 2 |
| Total Lines | 61 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import platform |
||
| 70 | def listbox_chooser(window_title: str, options: dict, default=None): # pragma: no cover |
||
| 71 | win = _modal_window(window_title) |
||
| 72 | |||
| 73 | choices = list(options.keys()) |
||
| 74 | |||
| 75 | choices_var = tk.Variable(win, value=choices) |
||
| 76 | |||
| 77 | selection_var = tk.Variable(win) |
||
| 78 | |||
| 79 | listbox_frame = ttk.Frame( |
||
| 80 | win, |
||
| 81 | # borderwidth=5, |
||
| 82 | # relief='solid', |
||
| 83 | ) |
||
| 84 | listbox_frame.pack(padx=2, pady=2) |
||
| 85 | |||
| 86 | # Creating a Listbox and |
||
| 87 | # attaching it to root window |
||
| 88 | listbox = tk.Listbox(listbox_frame, |
||
| 89 | listvariable=choices_var, |
||
| 90 | selectmode="MULTIPLE", |
||
| 91 | height=min(len(choices), 50), |
||
| 92 | width=max(len(c) for c in choices)) |
||
| 93 | |||
| 94 | # Adding Listbox to the left |
||
| 95 | # side of root window |
||
| 96 | listbox.pack(side=tk.LEFT, fill=tk.BOTH) |
||
| 97 | |||
| 98 | # Creating a Scrollbar and |
||
| 99 | # attaching it to root window |
||
| 100 | scrollbar = tk.Scrollbar(listbox_frame) |
||
| 101 | |||
| 102 | # Adding Scrollbar to the right |
||
| 103 | # side of root window |
||
| 104 | scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH) |
||
| 105 | |||
| 106 | # Attaching Listbox to Scrollbar |
||
| 107 | # Since we need to have a vertical |
||
| 108 | # scroll we use yscrollcommand |
||
| 109 | listbox.config(yscrollcommand=scrollbar.set) |
||
| 110 | |||
| 111 | # setting scrollbar command parameter |
||
| 112 | # to listbox.yview method its yview because |
||
| 113 | # we need to have a vertical view |
||
| 114 | scrollbar.config(command=listbox.yview) |
||
| 115 | |||
| 116 | def on_select(e): |
||
| 117 | print(e) |
||
| 118 | exit_button['state'] = tk.NORMAL |
||
| 119 | selection_var.set(listbox.curselection()) |
||
| 120 | |||
| 121 | listbox.bind("<<ListboxSelect>>", on_select) |
||
| 122 | # listbox.bind("<Double-1>", lambda e: selection_var.set(listbox.curselection())) |
||
| 123 | |||
| 124 | exit_button = ttk.Button(win, text="OK", command=win.destroy, state=tk.DISABLED) |
||
| 125 | exit_button.pack(pady=4, side=tk.BOTTOM) |
||
| 126 | |||
| 127 | win.mainloop() |
||
| 128 | |||
| 129 | for choice in selection_var.get(): |
||
| 130 | yield choice, options.get(choices[choice], default) |
||
| 131 | # choice = options.get(options.keys()[listbox.curselection()], default) |
||
| 134 |