-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialogs.py
131 lines (106 loc) · 4.25 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
from functools import partial
from sys import platform
from tkinter import TclError, Toplevel
from tkinter.ttk import Button, Frame, Label
class Messagebox(Toplevel):
def __init__(self, parent, title, details, icon, *, buttons) -> None:
super().__init__()
self.withdraw()
self.result: bool | None = None
self.big_frame = Frame(self)
self.big_frame.pack(fill="both", expand=True)
self.big_frame.columnconfigure(0, weight=1)
self.big_frame.rowconfigure(0, weight=1)
info_frame = Frame(self.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:
self.color = self.big_frame.tk.call("set", "themeColors::dialogInfoBg")
except TclError:
self.color = self.big_frame.tk.call(
"ttk::style", "lookup", "TFrame", "-background"
)
self.icon_label = Label(
info_frame, image=icon, anchor="nw", background=self.color
)
if icon is not None:
self.icon_label.grid(
row=0, column=0, sticky="nsew", padx=(12, 0), pady=10, rowspan=2
)
self.title_label = Label(
info_frame,
text=title,
anchor="nw",
font=("", 14, "bold"),
background=self.color,
)
self.title_label.grid(
row=0, column=1, sticky="nsew", padx=(12, 17), pady=(10, 8)
)
self.detail_label = Label(
info_frame, text=details, anchor="nw", background=self.color
)
self.detail_label.grid(
row=1, column=1, sticky="nsew", padx=(12, 17), pady=(5, 10)
)
self.button_frame = Frame(
self.big_frame, padding=(22, 22, 12, 22), style="Dialog_buttons.TFrame"
)
self.button_frame.grid(row=2, column=0, sticky="nsew")
for index, button_value in enumerate(buttons):
self.style = None
self.state = None
self.default = False
self.sticky = "nes" if len(buttons) == 1 else "nsew"
if len(button_value) > 2:
if button_value[2] == "accent":
self.style = "Accent.TButton"
self.default = True
elif button_value[2] == "disabled":
self.state = "disabled"
elif button_value[2] == "default":
self.default = True
self.button = Button(
self.button_frame,
text=button_value[0],
width=18,
command=partial(self.on_button, button_value[1]),
style=self.style, # type: ignore
state=self.state, # type: ignore
)
if self.default:
self.button.bind("<Return>", self.button["command"])
self.button.focus()
self.button.grid(row=0, column=index, sticky=self.sticky, padx=(0, 10))
self.button_frame.columnconfigure(index, weight=1)
self.update_idletasks()
self.dialog_width = self.winfo_width()
self.dialog_height = self.winfo_height()
if parent is None:
self.parent_width = self.winfo_screenwidth()
self.parent_height = self.winfo_screenheight()
self.parent_x = 0
self.parent_y = 0
else:
self.parent_width = parent.winfo_width()
self.parent_height = parent.winfo_height()
self.parent_x = parent.winfo_x()
self.parent_y = parent.winfo_y()
self.x_coord = int(
self.parent_width / 2 + self.parent_x - self.dialog_width / 2
)
self.y_coord = int(
self.parent_height / 2 + self.parent_y - self.dialog_height / 2
)
self.geometry("+{}+{}".format(self.x_coord, self.y_coord))
self.minsize(320, self.dialog_height)
self.transient(parent)
if platform != "win32":
self.wm_attributes("-type", "dialog")
self.grab_set()
self.deiconify()
self.wait_window()
def on_button(self, value):
self.result = value
self.destroy()