-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddEditWindow.py
More file actions
188 lines (177 loc) · 6.92 KB
/
AddEditWindow.py
File metadata and controls
188 lines (177 loc) · 6.92 KB
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
188
from logging import warning
from collections.abc import Callable
from tkinter import Tk, Toplevel, ttk, filedialog, messagebox, END
from tkinter.simpledialog import askstring
from Helpers import GetExt
from pathlib import Path
class AddEditWindow(Toplevel):
def __init__(
self,
root: Tk,
currProfile: list[str],
ok_callback: Callable[["AddEditWindow"], None],
title: str = None,
):
super().__init__(root)
root.wm_attributes("-disabled", True)
self.wm_protocol("WM_DELETE_WINDOW", self.on_close)
self.root = root
self.style = ttk.Style(self)
self.style.configure("Edit.TButton", font=("Arial", 10), width=7)
self.configure(bg=self.style.lookup("Tk", "background"))
self.resizable(False, False)
self.wm_title(title)
self.profile_added = False
self.profile_updated = False
self.currProfile = currProfile
# get coords and dimensions of root window
coords = root.geometry().split("+")
w, h = list(map(int, coords[0].split("x")))
coords.pop(0)
x, y = list(map(int, coords))
# set the geometry of window to half that of root
# and spawn it centered with it
self.geometry(f"350x300+{x+((w//2)-175)}+{y+((h//2)-150)}")
self.grid_columnconfigure((0, 1), weight=1)
ttk.Label(self, text="Profile name: ").grid(
row=0, column=0, padx=5, pady=5, sticky="w"
)
self.entry_profile = ttk.Entry(self, state="readonly", width=40)
self.entry_profile.grid(
row=1, column=0, columnspan=3, padx=5, pady=5, sticky="w"
)
ttk.Label(self, text="Personal saves folder: ").grid(
row=2, column=0, padx=5, pady=5, sticky="w"
)
self.entry_p = ttk.Entry(self, state="readonly", width=40)
self.entry_p.grid(row=3, column=0, columnspan=3, padx=5, pady=5, sticky="w")
ttk.Label(self, text="Game's saves folder: ").grid(
row=4, column=0, padx=5, pady=5, sticky="w"
)
self.entry_g = ttk.Entry(self, state="readonly", width=40)
self.entry_g.grid(row=5, column=0, columnspan=3, padx=5, pady=5, sticky="w")
ttk.Label(self, text="File extentsion: ").grid(
row=6, column=0, padx=5, pady=5, sticky="w"
)
self.entry_ext = ttk.Entry(self, state="readonly", width=40)
self.entry_ext.grid(row=7, column=0, columnspan=3, padx=5, pady=5, sticky="w")
self.button_profile = ttk.Button(
self,
text="Choose",
style="Edit.TButton",
command=lambda: self.Choose(0),
)
self.button_personal_folder = ttk.Button(
self,
text="Choose",
style="Edit.TButton",
command=lambda: self.Choose(1),
)
self.button_game_folder = ttk.Button(
self,
text="Choose",
style="Edit.TButton",
command=lambda: self.Choose(2),
)
self.button_extension = ttk.Button(
self,
text="Choose",
style="Edit.TButton",
command=lambda: self.Choose(3),
)
self.button_profile.grid(row=1, column=3, padx=5, pady=5, sticky="e")
self.button_personal_folder.grid(row=3, column=3, padx=5, pady=5, sticky="e")
self.button_game_folder.grid(row=5, column=3, padx=5, pady=5, sticky="e")
self.button_extension.grid(row=7, column=3, padx=5, pady=5, sticky="e")
ttk.Button(
self,
text="OK",
style="Edit.TButton",
command=lambda: ok_callback(self),
).grid(row=8, column=0, padx=5, pady=5)
ttk.Button(self, text="Cancel", style="Edit.TButton", command=self.cancel).grid(
row=8, column=1, padx=5, pady=5
)
self.focus_force()
self.grab_set()
# self.wait_window()
def cancel(self):
self.profile_added = False
self.profile_updated = False
self.on_close()
def Choose(self, index: int):
if index == 0:
while True:
new = askstring(
"Profile Name",
"Choose a profile name",
parent=self,
initialvalue=self.entry_profile.get(),
)
if new == "Add...":
warning(f"Invalid name choice '{new}'")
messagebox.showwarning(
"Invalid Name", "Please choose another profile name"
)
continue
else:
break
if new:
self.entry_profile.state(["!readonly"])
self.entry_profile.delete(0, END)
self.entry_profile.insert(0, new)
self.entry_profile.state(["readonly"])
elif index == 1:
startDir = self.currProfile[index] if self.currProfile else ""
new = filedialog.askdirectory(
initialdir=startDir,
parent=self,
title="Choose your OWN personal saves folder",
)
if new:
self.entry_p.state(["!readonly"])
self.entry_p.delete(0, END)
self.entry_p.insert(0, new)
self.entry_p.state(["readonly"])
elif index == 2:
startDir = self.currProfile[index] if self.currProfile else ""
new = filedialog.askdirectory(
initialdir=startDir,
parent=self,
title="Choose the GAME'S saves folder",
)
if new:
self.entry_g.state(["!readonly"])
self.entry_g.delete(0, END)
self.entry_g.insert(0, new)
self.entry_g.state(["readonly"])
elif index == 3:
new = askstring(
"File Extension",
"Enter the file extension of the savefiles e.g. .sgd,.save,...\n For files with no extension use '*'",
parent=self,
)
if new:
if new == "*":
new = ""
elif not new.startswith(".") and len(new) >= 1:
new = "." + new
self.entry_ext.state(["!readonly"])
self.entry_ext.delete(0, END)
self.entry_ext.insert(0, new)
self.entry_ext.state(["readonly"])
if not new:
self.grab_set()
self.wait_window()
return
if not self.entry_ext.get() and (index == 1 or index == 2):
self.entry_ext.state(["!readonly"])
self.entry_ext.delete(0, END)
self.entry_ext.insert(0, GetExt(Path(new)))
self.entry_ext.state(["readonly"])
self.grab_set()
self.wait_window()
def on_close(self):
self.root.wm_attributes("-disabled", False)
self.root.focus_set()
self.destroy()