-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.py
613 lines (478 loc) · 17.2 KB
/
step.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
import os
from pathlib import Path
from tkinter import Tk, Canvas, Entry, PhotoImage,StringVar, messagebox, ttk
import openpyxl
from calculation import create_file, create_sheet_window, create_source_sheet, delete_default_sheet, fill_the_cells, calculations
from openpyxl import load_workbook
import argparse
from PIL import Image, ImageTk
import tkinter as tk
# Global variables
workbook = None
sheet_names = []
current_sheet_index = 0
notebook = None
experts = 5
max_percent = 10
min_percent = 3
step = 1
currentPercent = 0
selectedSheet = 1
def relative_to_assets(path: str) -> Path:
current_path = Path(__file__).parent.absolute() # Get the current directory
assets_path = current_path / Path("assets/frame2") # Set assets path
return assets_path / Path(path)
# receive the file_path as a param from main.py file
def parse_arguments():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument("--file", help="File path")
return parser.parse_args()
args = parse_arguments()
file_path = args.file if args.file else "table.xlsx"
# Check for change on the Expert input
def saveExperts(*args):
# Input is a valid number
canvas.itemconfigure(image_8_active, state="normal")
canvas.itemconfigure(image_8, state="hidden")
if int(entry_var_1.get()) <= 0:
messagebox.showwarning("Error", "Пожалуйста, введите число больше 0.")
# Set the Number of Experts
def NumofExperts():
global experts
try:
value = int(entry_var_1.get())
if int(entry_var_1.get()) <= 0:
messagebox.showwarning("Error", "Пожалуйста, введите число больше 0.")
else:
if value != experts:
experts = value
remove_excel_file()
create_file(file_path)
create_source_sheet(file_path, 1, experts, needed=True,distr=None)
delete_default_sheet(file_path)
LoadSheet()
canvas.itemconfigure(image_8_active, state="hidden")
canvas.itemconfigure(image_8, state="normal")
except ValueError:
# Input is not a valid number
messagebox.showwarning("Error", "Пожалуйста, введите числовые значения.")
# Genearate Random data for the given sheet
def generateRand():
current_tab = notebook.tab(notebook.select(), "text")
numeric_value = ''.join(filter(str.isdigit, current_tab))
fill_the_cells(file_path,numeric_value,experts)
LoadSheet()
# Load the Excel Sheets
def LoadSheet():
global workbook, sheet_names, notebook, current_sheet_index
# Load the workbook
workbook = load_workbook(file_path)
sheet_names = workbook.sheetnames
# Create the first sheet window
notebook = ttk.Notebook(window)
# Create a tab for each sheet and display the content
for sheet_name in sheet_names:
create_sheet_window(sheet_name, workbook, notebook, file_path, canvas,image_6_active)
# Select the first sheet by default
notebook.select(current_sheet_index)
# Place the Notebook widget at the position and size of image_5
x = 64
y = 120.0
width = 1421
height = 474
notebook.place(x=x, y=y, width=width, height=height)
# Define the event handler function
def check_selected_tab(event):
global current_sheet_index,selectedSheet
current_tab = notebook.tab(notebook.select(), "text")
current_tab_number = int(''.join(filter(str.isdigit, current_tab)))
has_higher_tabs = False
# Iterate over the tabs and check if there are higher number tabs
for tab_id in notebook.tabs():
tab_text = notebook.tab(tab_id, "text")
tab_number = int(''.join(filter(str.isdigit, tab_text)))
if tab_number > current_tab_number:
has_higher_tabs = True
break
if current_tab.startswith("Исход") and not has_higher_tabs:
if (currentPercent == 0 or currentPercent is None) or ( currentPercent > float(entry_var_2.get()) or currentPercent < float(entry_var_3.get())):
canvas.itemconfigure(image_7_active, state="normal")
canvas.itemconfigure(image_7, state="hidden")
canvas.itemconfigure(image_10_active, state="hidden")
else:
canvas.itemconfigure(image_7_active, state="hidden")
canvas.itemconfigure(image_7, state="normal")
else:
if current_tab.startswith("Распределенные"):
selectedSheet = int(current_tab.split()[-2])
canvas.itemconfigure(image_10_active, state="normal")
else:
canvas.itemconfigure(image_10_active, state="normal")
canvas.itemconfigure(image_7_active, state="hidden")
canvas.itemconfigure(image_7, state="normal")
current_sheet_index = notebook.index(notebook.select())
# Bind the event handler to the NotebookTabChanged event
notebook.bind("<<NotebookTabChanged>>", check_selected_tab)
# Handle Next Step (Display Next sheet)
def handle_next(notebook):
global current_sheet_index
current_sheet_index += 1
if current_sheet_index >= len(sheet_names):
current_sheet_index = 0
notebook.select(current_sheet_index)
# Handle Previous Step (Display Previous sheet)
def handle_previous(notebook):
global current_sheet_index
current_sheet_index -= 1
if current_sheet_index < 0:
current_sheet_index = len(sheet_names) - 1
notebook.select(current_sheet_index)
# Calculation for Each Step
def NextStep(max_possible_percent,min_possible_percent):
global currentPercent, step
if (currentPercent == 0 or currentPercent is None) or ( currentPercent > max_possible_percent or currentPercent < min_possible_percent):
if step < 6:
currentPercent = calculations(file_path, step,int(entry_var_4.get()),experts, os.path.dirname(file_path))
# Function to open a new tkinter window and display images
def open_image_window():
global selectedSheet
# Create a new tkinter window
image_window = tk.Toplevel()
image_window.title(f"Графики Распределение Щаг {selectedSheet}")
# Create a canvas widget
canvas = tk.Canvas(image_window)
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Create a scrollbar and associate it with the canvas
scrollbar = tk.Scrollbar(image_window, command=canvas.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
canvas.configure(yscrollcommand=scrollbar.set)
# Create a frame inside the canvas
image_frame = tk.Frame(canvas)
canvas.create_window((0, 0), window=image_frame, anchor=tk.NW)
# Load and display images
image_paths = []
for i in range(1, experts + 1):
image_path = os.path.dirname(file_path) + f"/распределение_{selectedSheet}_шаг_{i}_эксперт.png"
image_paths.append(image_path)
for path in image_paths:
image = Image.open(path)
photo = ImageTk.PhotoImage(image)
# Create a label to display the image
label = tk.Label(image_frame, image=photo)
label.image = photo
label.pack()
# Update the canvas scroll region
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox("all"))
image_window.geometry(f"650x600")
# Check if the data on the sheet are correct and valid
def check_data():
if(step < 6):
wb = openpyxl.load_workbook(file_path)
sheet_name = f'Исходные данные {step} шага'
sheet = wb[sheet_name]
for row_num in range(2, experts + 2):
for column_num in range(2, 5):
cell = sheet.cell(row=row_num, column=column_num)
if cell.value is None:
messagebox.showwarning("Error", f'в ячейке отсутствуют данные ({cell.coordinate})')
wb.close()
return False
elif not isinstance(cell.value, (float, int)):
messagebox.showwarning("Error", f'Исходные данные {step} шага не верны или не правильном формате')
wb.close()
return False
wb.close()
return True
# Function responsible for Sheet Creation for each step and input validation
def validate_inputs():
global step, current_sheet_index, currentPercent, file_path
all_numeric = all(var.get().isdigit() for var in input_vars)
if not all_numeric:
messagebox.showwarning("Error", "Пожалуйста, введите числовые значения.")
else:
entry_1.config(state='disabled', disabledbackground='#0F0D1D', disabledforeground='#878787')
entry_2.config(state='disabled', disabledbackground='#0F0D1D', disabledforeground='#878787')
entry_3.config(state='disabled', disabledbackground='#0F0D1D', disabledforeground='#878787')
entry_4.config(state='disabled', disabledbackground='#0F0D1D', disabledforeground='#878787')
if (currentPercent != 0 and currentPercent is not None) and currentPercent < float(entry_var_2.get()) and currentPercent > float(entry_var_3.get()):
handle_next(notebook)
else:
if check_data():
if step <=5:
NextStep(float(entry_var_2.get()), float(entry_var_3.get()))
if (currentPercent == 0 or currentPercent is None) or ( currentPercent > float(entry_var_2.get()) or currentPercent < float(entry_var_3.get())):
step+=1
if step <6:
create_source_sheet(file_path, step,experts,needed=True)
current_sheet_index += 1
LoadSheet()
canvas.itemconfigure(image_9_active, state="normal")
canvas.itemconfigure(image_9, state="hidden")
# for filename in os.listdir(os.path.dirname(file_path)):
# if filename.startswith("распределение") and filename.endswith((".png")):
# file = os.path.join(os.path.dirname(file_path), filename)
# os.remove(file)
# Delete an excel file after Reset
def remove_excel_file():
if os.path.exists(file_path):
os.remove(file_path)
# Reset all the data and remove the sheets
def resetAll():
global current_sheet_index, step, currentPercent
current_sheet_index = 0
step = 1
currentPercent = 0
remove_excel_file()
create_file(file_path)
create_source_sheet(file_path, 1, experts, needed=True)
delete_default_sheet(file_path)
LoadSheet()
entry_1.config(state='normal')
entry_2.config(state='normal' )
entry_3.config(state='normal')
entry_4.config(state='normal')
entry_var_1.set(f"{experts}")
entry_var_2.set("10")
entry_var_3.set("3")
entry_var_4.set("1000")
window = Tk()
window.title("ПУЗ-ОРХ")
style = ttk.Style(window)
window.tk.call("source", "assets/forest-dark.tcl")
style.theme_use("forest-dark")
window.geometry("1541x832")
window.configure(bg = "#06010F")
canvas = Canvas(
window,
bg = "#06010F",
height = 832,
width = 1541,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
canvas.place(x = 0, y = 0)
image_image_1 = PhotoImage(
file=relative_to_assets("image_1.png"))
image_1 = canvas.create_image(
771.0,
669.0,
image=image_image_1
)
image_image_2 = PhotoImage(
file=relative_to_assets("image_2.png"))
image_2 = canvas.create_image(
1281.0,
768.0,
image=image_image_2
)
image_image_3 = PhotoImage(
file=relative_to_assets("image_3.png"))
image_3 = canvas.create_image(
1416.0,
773.0,
image=image_image_3
)
image_image_4 = PhotoImage(
file=relative_to_assets("image_4.png"))
image_4 = canvas.create_image(
932.0,
372.0,
image=image_image_4
)
image_image_5 = PhotoImage(
file=relative_to_assets("image_5.png"))
image_5 = canvas.create_image(
775.0,
659.0,
image=image_image_5
)
image_image_6 = PhotoImage(
file=relative_to_assets("image_6.png"))
image_6 = canvas.create_image(
1346.0,
660.0,
image=image_image_6
)
image_image_6_active = PhotoImage(
file=relative_to_assets("image_6_active.png"))
image_6_active = canvas.create_image(
1346.0,
660.0,
image=image_image_6_active,
)
image_image_7 = PhotoImage(
file=relative_to_assets("image_7.png"))
image_7 = canvas.create_image(
1168.0,
660.0,
image=image_image_7
)
image_image_7_active = PhotoImage(
file=relative_to_assets("image_7_active.png"))
image_7_active = canvas.create_image(
1168.0,
660.0,
image=image_image_7_active
)
entry_image_1 = PhotoImage(
file=relative_to_assets("entry_1.png"))
entry_bg_1 = canvas.create_image(
181.5,
83.5,
image=entry_image_1
)
entry_var_1 = StringVar()
entry_var_1.set("5")
entry_1 = Entry( bd=0, bg="#0F0D1D", fg='white', font=("Arial", 18), highlightthickness=0,highlightcolor='white'
,highlightbackground='white',textvariable=entry_var_1)
entry_1.place(
x=106.0,
y=56.0,
width=151.0,
height=53.0
)
canvas.create_text(
91.0,
23.0,
anchor="nw",
text="Кол-во экспертов",
fill="#FFFFFF",
font=("PlusJakartaSansRoman SemiBold", 17 * -1)
)
image_image_8 = PhotoImage(
file=relative_to_assets("image_8.png"))
image_8 = canvas.create_image(
388.0,
83.0,
image=image_image_8
)
image_image_8_active = PhotoImage(
file=relative_to_assets("image_8_active.png"))
image_8_active = canvas.create_image(
388.0,
83.0,
image=image_image_8_active,
state='hidden'
)
canvas.create_text(
504.0,
23.0,
anchor="nw",
text="Максимальный процент",
fill="#FFFFFF",
font=("PlusJakartaSansRoman SemiBold", 17 * -1)
)
entry_image_2 = PhotoImage(
file=relative_to_assets("entry_2.png"))
entry_bg_2 = canvas.create_image(
596.5,
83.5,
image=entry_image_2
)
entry_var_2 = StringVar()
entry_var_2.set("10")
entry_2 = Entry( bd=0, bg="#0F0D1D", fg='white', font=("Arial", 18), highlightthickness=0,highlightcolor='white',
highlightbackground='white' , textvariable=entry_var_2)
entry_2.place(
x=519.0,
y=56.0,
width=151.0,
height=53.0
)
entry_image_3 = PhotoImage(
file=relative_to_assets("entry_3.png"))
entry_bg_3 = canvas.create_image(
830.5,
83.5,
image=entry_image_3
)
entry_var_3 = StringVar()
entry_var_3.set("3")
entry_3 = Entry( bd=0, bg="#0F0D1D", fg='white', font=("Arial", 18), highlightthickness=0,highlightcolor='white',
highlightbackground='white', textvariable=entry_var_3)
entry_3.place(
x=755.0,
y=56.0,
width=151.0,
height=53.0
)
canvas.create_text(
745.0,
23.0,
anchor="nw",
text="Минимальный процент",
fill="#FFFFFF",
font=("PlusJakartaSansRoman SemiBold", 17 * -1)
)
entry_image_4 = PhotoImage(
file=relative_to_assets("entry_3.png"))
entry_bg_4 = canvas.create_image(
1060,
83.5,
image=entry_image_4
)
entry_var_4 = StringVar()
entry_var_4.set("1000")
entry_4 = Entry( bd=0, bg="#0F0D1D", fg='white', font=("Arial", 18), highlightthickness=0,highlightcolor='white',
highlightbackground='white', textvariable=entry_var_4)
entry_4.place(
x=987.0,
y=56.0,
width=151.0,
height=53.0
)
canvas.create_text(
990.0,
23.0,
anchor="nw",
text="Кол-во Итерации",
fill="#FFFFFF",
font=("PlusJakartaSansRoman SemiBold", 17 * -1)
)
image_image_9 = PhotoImage(
file=relative_to_assets("image_9.png"))
image_9 = canvas.create_image(
1350.0,
72.0,
image=image_image_9
)
image_image_9_active = PhotoImage(
file=relative_to_assets("image_9_active.png"))
image_9_active = canvas.create_image(
1350.0,
72.0,
image=image_image_9_active,
state='hidden'
)
input_vars = [entry_var_1, entry_var_2, entry_var_3,entry_var_4] # List of input fields
entry_var_1.trace_add("write", saveExperts) # Bind trace to each entry
image = Image.open(relative_to_assets("graphic.png"))
# Resize the image with the desired width and height
new_width = 170
new_height = 50
resized_image = image.resize((new_width, new_height))
# Convert the resized image to a PhotoImage object
image_image_10_active = ImageTk.PhotoImage(resized_image)
# Create the image on the canvas
image_10_active = canvas.create_image(
600.0,
660.0,
image=image_image_10_active,
state='hidden'
)
saveExperts()
LoadSheet()
canvas.tag_raise(image_3)
canvas.tag_raise(image_2)
canvas.tag_raise(image_2)
canvas.tag_bind(image_3, "<Button-1>", lambda event: validate_inputs())
canvas.tag_bind(image_8_active, "<Button-1>", lambda event: NumofExperts())
canvas.tag_bind(image_7_active, "<Button-1>", lambda event: generateRand())
canvas.tag_bind(image_10_active, "<Button-1>", lambda event: open_image_window())
canvas.tag_bind(image_9_active, "<Button-1>", lambda event: resetAll())
canvas.tag_bind(image_2, "<Button-1>", lambda event: handle_previous(notebook))
window.resizable(False, False)
window.mainloop()