-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogs.py
137 lines (107 loc) · 4.01 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
import tkinter as tk
from tkinter import ttk
from functools import partial
import sv_ttk
def show_message(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Yes", None, "default")],
)
def ask_yes_no(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("开启", True, "accent"), ("关闭", False)],
)
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=("", 24, "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, font=("", 15), 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)
dialog.overrideredirect(True)
dialog.update()
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) + 250
y_coord = int(parent_height / 2 + parent_y - dialog_height / 2) + 50
dialog.geometry("+{}+{}".format(x_coord, y_coord))
dialog.minsize(600, 300)
dialog.transient(parent)
dialog.grab_set()
dialog.wait_window()
return result
if __name__ == "__main__":
window = tk.Tk()
sv_ttk.use_light_theme()
window.geometry("600x300")
window.mainloop()