-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
225 lines (176 loc) · 6.55 KB
/
gui.py
File metadata and controls
225 lines (176 loc) · 6.55 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import ttkbootstrap as tb
from ttkbootstrap.constants import *
from pathlib import Path
from tkinter import messagebox, filedialog
import logging
from tutorialcreator.renderer import OutputFormat
import tutorialcreator as tc
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
class TutorialCreatorGUI:
def __init__(self, root: tb.Window):
self.root = root
self.root.title("Tutorial Creator")
self.root.geometry("1200x750")
self.root.minsize(950, 650)
logger.info("Starting ttkbootstrap GUI")
# State variables
self.module_var = tb.StringVar(value="STSM3714")
self.tutorial_var = tb.StringVar()
self.output_var = tb.StringVar(value="pdf")
self.seed_var = tb.StringVar(value="42")
self.header_vars = {}
self.cfg = None
self.tut = None
self.build_layout()
self.load_tutorial()
def build_layout(self):
# Main container
container = tb.Frame(self.root, padding=20)
container.pack(fill=BOTH, expand=True)
# Title
tb.Label(
container,
text="Statistics Tutorial Creator",
font=("Comic Sans", 50,"bold"),
bootstyle="primary"
).pack(pady=(0, 20))
# Top controls
top_frame = tb.Frame(container)
top_frame.pack(fill=X, pady=10)
left_card = tb.Labelframe(
top_frame,
text="Configuration",
padding=15,
bootstyle="info"
)
left_card.pack(side=LEFT, fill=BOTH, expand=True, padx=10)
right_card = tb.Labelframe(
top_frame,
text="Seed & Render",
padding=15,
bootstyle="warning"
)
right_card.pack(side=LEFT, fill=BOTH, expand=True, padx=10)
# Configuration widgets
tb.Label(left_card, text="Module").pack(anchor=W)
module_box = tb.Combobox(
left_card,
textvariable=self.module_var,
values=("STSM3714", "STSM2624")
)
module_box.pack(fill=X, pady=5)
module_box.bind("<<ComboboxSelected>>", lambda e: self.load_tutorial())
tb.Label(left_card, text="Tutorial").pack(anchor=W)
self.tutorial_box = tb.Combobox(
left_card,
textvariable=self.tutorial_var,
values=tc.list_tutorials()
)
self.tutorial_box.pack(fill=X, pady=5)
self.tutorial_box.bind("<<ComboboxSelected>>", lambda e: self.load_tutorial())
tb.Label(left_card, text="Output Format").pack(anchor=W)
output_frame = tb.Frame(left_card)
output_frame.pack(fill=X, pady=5)
for fmt in ("pdf", "docx", "html"):
tb.Radiobutton(
output_frame,
text=fmt.upper(),
variable=self.output_var,
value=fmt,
bootstyle="info-toolbutton"
).pack(side=LEFT, padx=5)
# Seed widgets
tb.Label(right_card, text="Seed").pack(anchor=W)
tb.Entry(right_card, textvariable=self.seed_var).pack(fill=X, pady=5)
tb.Button(
right_card,
text="Render Tutorial",
command=self.render_click,
bootstyle="success"
).pack(pady=20, fill=X)
# Bottom content area
bottom_frame = tb.Frame(container)
bottom_frame.pack(fill=BOTH, expand=True, pady=15)
self.header_frame = tb.Labelframe(
bottom_frame,
text="Header Configuration",
padding=15,
bootstyle="secondary"
)
self.header_frame.pack(side=LEFT, fill=BOTH, expand=True, padx=10)
def load_tutorial(self):
try:
tutorial_id = self.tutorial_var.get() or tc.list_tutorials()[0]
self.tutorial_var.set(tutorial_id)
self.tut, self.cfg = tc.select_tutorial(
subject=self.module_var.get(),
tutorial_id=tutorial_id
)
self.cfg.set_seed(seed=int(self.seed_var.get()))
self.build_header_editor()
except Exception as e:
logger.exception("Failed to load tutorial")
messagebox.showerror("Error", str(e))
def build_header_editor(self):
for widget in self.header_frame.winfo_children():
widget.destroy()
header = self.cfg.get_header()
self.header_vars.clear()
fields = [
("title", "Title"),
("author", "Author"),
("university", "University"),
("department", "Department"),
("module_code", "Module Code"),
("module_name", "Module Name"),
("due_date", "Due Date"),
]
for key, label in fields:
tb.Label(self.header_frame, text=label).pack(anchor=W)
var = tb.StringVar(value=header.get(key, ""))
self.header_vars[key] = var
tb.Entry(self.header_frame, textvariable=var).pack(fill=X, pady=4)
tb.Label(self.header_frame, text="Total Marks").pack(anchor=W, pady=(10, 0))
marks_var = tb.StringVar(value=str(header.get("total_marks", 50)))
self.header_vars["total_marks"] = marks_var
tb.Entry(
self.header_frame,
textvariable=marks_var
).pack(fill=X, pady=5)
tb.Button(
self.header_frame,
text="Update Header",
command=self.update_header,
bootstyle="primary"
).pack(fill=X, pady=15)
def update_header(self):
header_data = {
key: var.get() for key, var in self.header_vars.items()
}
self.cfg.set_header(**header_data)
def render_click(self):
try:
logger.info("Rendering tutorial")
self.cfg.set_seed(seed=int(self.seed_var.get()))
output_map = {
"pdf": OutputFormat.PDF,
"html": OutputFormat.HTML,
"docx": OutputFormat.WORD,
}
if hasattr(self,'output_dir'):
self.tut.output_dir = self.output_dir
print(self.tut.output_dir)
tc.build(
self.tut,
self.cfg,
output_map[self.output_var.get()]
)
messagebox.showinfo("Success", "Tutorial rendered successfully!")
except Exception as e:
logger.exception("Render failed")
messagebox.showerror("Error", str(e))
if __name__ == "__main__":
app = tb.Window(themename="darkly")
TutorialCreatorGUI(app)
app.mainloop()