Passed
Push — main ( b7a647...25fd53 )
by Eran
03:14
created

graphinate.tools.gui.radiobutton_chooser()   A

Complexity

Conditions 2

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 30
nop 3
dl 0
loc 48
rs 9.16
c 0
b 0
f 0
1
import platform
2
import tkinter as tk
3
from tkinter import ttk as ttk
4
5
6
def _modal_window(title: str) -> tk.Tk:  # pragma: no cover
7
    """Creating a parent Tkinter modal window"""
8
    win = tk.Tk()
9
    # win.geometry("200x125")
10
    win.title(title)
11
12
    if platform.system().lower() == 'windows':
13
        win.wm_attributes('-toolwindow', 'True')
14
15
    win.wm_attributes('-topmost', 'True')
16
    win.resizable(False, False)
17
    return win
18
19
20
def radiobutton_chooser(window_title: str, options: dict, default=None):  # pragma: no cover
21
    win = _modal_window(window_title)
22
23
    # let us create a Tkinter string variable
24
    # that is able to store any string value
25
    choice_var = tk.Variable(win, None)
26
27
    radiobuttons_frame = ttk.Frame(
28
        win,
29
        borderwidth=5,
30
        relief='solid',
31
    )
32
    radiobuttons_frame.pack(padx=4, pady=4)
33
34
    ttk.Label(
35
        radiobuttons_frame,
36
        text="Output Mode:",
37
        # font=('Helvetica 9 bold'),
38
        # foreground="red3"
39
    ).pack()
40
41
    def change_state():
42
        exit_button['state'] = tk.NORMAL
43
44
    for option in options:
45
        ttk.Radiobutton(
46
            radiobuttons_frame,
47
            text=option,
48
            variable=choice_var,
49
            value=option,
50
            command=change_state,
51
            padding=4
52
        ).pack(
53
            side=tk.TOP,
54
            # ipady=1,
55
            anchor="w"
56
        )
57
58
    exit_button = ttk.Button(win, text="OK", command=win.destroy, state=tk.DISABLED)
59
    exit_button.pack(pady=4, side=tk.BOTTOM)
60
61
    # sv_ttk.set_theme("dark")
62
63
    win.mainloop()
64
65
    choice = choice_var.get()
66
67
    return choice, options.get(choice, default)
68
69
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)
132
133
    # return choice, options.get(choice, default)
134