-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialogs.py
187 lines (145 loc) · 5.17 KB
/
dialogs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import sys
import tkinter as tk
from tkinter import ttk
from functools import partial
def popup(parent, title, details, icon, *, buttons):
dialog = tk.Toplevel()
result = None
big_frame = ttk.Frame(dialog)
big_frame.pack(fill="both", expand=True)
big_frame.columnconfigure(0, weight=1)
big_frame.rowconfigure(0, weight=1)
info_frame = ttk.Frame(big_frame, padding=(10, 12), style="Dialog_info.TFrame")
info_frame.grid(row=0, column=0, sticky="nsew")
info_frame.columnconfigure(1, weight=1)
info_frame.rowconfigure(1, weight=1)
try:
color = big_frame.tk.call("set", "themeColors::dialogInfoBg")
except tk.TclError:
color = big_frame.tk.call("ttk::style", "lookup", "TFrame", "-background")
icon_label = ttk.Label(info_frame, image=icon, anchor="nw", background=color)
if icon is not None:
icon_label.grid(
row=0, column=0, sticky="nsew", padx=(12, 0), pady=10, rowspan=2
)
title_label = ttk.Label(
info_frame, text=title, anchor="nw", font=("", 14, "bold"), background=color
)
title_label.grid(row=0, column=1, sticky="nsew", padx=(12, 17), pady=(10, 8))
detail_label = ttk.Label(info_frame, text=details, anchor="nw", background=color)
detail_label.grid(row=1, column=1, sticky="nsew", padx=(12, 17), pady=(5, 10))
button_frame = ttk.Frame(
big_frame, padding=(22, 22, 12, 22), style="Dialog_buttons.TFrame"
)
button_frame.grid(row=2, column=0, sticky="nsew")
def on_button(value):
nonlocal result
result = value
dialog.destroy()
for index, button_value in enumerate(buttons):
style = None
state = None
default = False
sticky = "nes" if len(buttons) == 1 else "nsew"
if len(button_value) > 2:
if button_value[2] == "accent":
style = "Accent.TButton"
default = True
elif button_value[2] == "disabled":
state = "disabled"
elif button_value[2] == "default":
default = True
button = ttk.Button(
button_frame,
text=button_value[0],
width=18,
command=partial(on_button, button_value[1]),
style=style,
state=state,
)
if default:
button.bind("<Return>", button["command"])
button.focus()
button.grid(row=0, column=index, sticky=sticky, padx=(0, 10))
button_frame.columnconfigure(index, weight=1)
if sys.platform == "win32":
transparent_color = big_frame.tk.call("ttk::style", "lookup", "TFrame", "-background")
dialog.wm_attributes("-transparentcolor", transparent_color)
dialog.overrideredirect(True)
dialog.update_idletasks()
dialog_width = dialog.winfo_width()
dialog_height = dialog.winfo_height()
if parent is None:
parent_width = dialog.winfo_screenwidth()
parent_height = dialog.winfo_screenheight()
parent_x = 0
parent_y = 0
else:
parent_width = parent.winfo_width()
parent_height = parent.winfo_height()
parent_x = parent.winfo_x()
parent_y = parent.winfo_y()
x_coord = int(parent_width / 2 + parent_x - dialog_width / 2)
y_coord = int(parent_height / 2 + parent_y - dialog_height / 2)
dialog.geometry("+{}+{}".format(x_coord, y_coord))
dialog.minsize(320, dialog_height)
dialog.transient(parent)
dialog.wm_attributes("-type", "dialog")
dialog.grab_set()
dialog.wait_window()
return result
def show_message(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Ok", None, "default")],
)
def ask_ok_cancel(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Ok", True, "accent"), ("Cancel", None)],
)
def ask_yes_no(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Yes", True, "accent"), ("No", False)],
)
def ask_yes_no_cancel(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Yes", True, "accent"), ("No", False), ("Cancel", None)],
)
def ask_retry_cancel(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Retry", True, "accent"), ("Cancel", None)],
)
def ask_allow_block(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Allow", True, "accent"), ("Block", False)],
)
if __name__ == "__main__":
window = tk.Tk()
window.tk.call("source", "sun-valley.tcl")
window.tk.call("set_theme", "dark")
window.geometry("600x600")
show_message("No WiFi connection", "Check your connection and try again.")
window.mainloop()