-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStochasticPolicyPage.py
More file actions
233 lines (181 loc) · 10.5 KB
/
Copy pathStochasticPolicyPage.py
File metadata and controls
233 lines (181 loc) · 10.5 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
226
227
228
229
230
231
232
233
import tkinter as tk
from tkinter import ttk
import os
from tkinter import END
from tkinter import messagebox as msgbox
from tkinter import filedialog
LARGEFONT = ("Verdana", 24)
class StochasticPolicyPage(tk.Frame):
path = ''
listBox : tk.Listbox
def refreshListBox(self, controller): #very ugly way to update items in the listbox
if self.path == '':
msgbox.showerror("error", "Please select a valid path!\n")
controller.show_mainPage()
else:
file_list = [file for file in os.listdir(self.path) if file.endswith(".sdl") or file.endswith(".tdl")]
self.listBox.delete(0, END)
for file in file_list:
self.listBox.insert(END,str(file))
def emptyListBox(self):
self.listBox.delete(0, END) #this ensures that the listbox will be empty every time the frame is loaded, before choosing the new directory
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.grid_columnconfigure(0, weight=1)
style = ttk.Style()
style.configure('CustomButton.TButton', font=('Arial', 19))
file_list = [file for file in os.listdir("./saved_models") if file.endswith(".sdl") or file.endswith(".tdl")]
sdl_list = [file for file in os.listdir("./templates") if file.endswith(".sdl")]
tdl_list = [file for file in os.listdir("./templates") if file.endswith(".tdl")]
def reset():
inputtxt.delete(1.0, END)
self.listBox.delete(0, END)
#loadedFileLabel.config(text = "here is the list of all loaded files: \n")
def refreshListBox(controller): #very ugly way to update items in the listbox
#path = str('./' + str(loadFile())) #TODO right now it crashes and it loads at the very begin with no apparent reason
file_list = [file for file in os.listdir(self.path) if file.endswith(".sdl") or file.endswith(".tdl")]
self.listBox.delete(0, END)
for file in file_list:
self.listBox.insert(END,str(file))
def delete_item(): #function called by button DELETE
selected_index = self.listBox.curselection()
selected_value = self.listBox.get(selected_index)
subfolder = "saved_models"
file_path = os.path.join(subfolder, selected_value)
if selected_index:
flag = msgbox.askyesno("Are you sure?", "do you want to permanently delete "+selected_value+" ?")
print(flag)
if os.path.isfile(file_path) and flag:
file_list.remove(selected_value)
os.remove(str(file_path))
msgbox.showinfo("all gone"," File "+selected_value+ "has been deleted")
self.listBox.delete(selected_index)
reset()
refreshListBox(controller)
def showFile(): #action of button LOAD
inputtxt.delete(1.0, END) #first we clean the textbox
selected_index = self.listBox.curselection()
selected_value = self.listBox.get(selected_index)
subfolder = "saved_models"
file_path = os.path.join(subfolder, selected_value)
with open(str(file_path), "r") as file:
inputtxt.insert(1.0,str(file.read()))
def loadSDLFiles():
inputtxt.delete(1.0, END)
for file in sdl_list:
subfolder = "templates"
file_path = os.path.join(subfolder, str(file))
with open(str(file_path), "r") as file:
inputtxt.insert(END,str(file.read()+ '\n')) #A NEWLINE IS INSERTED FOR EVERY NEW LOADED FILE
def loadTDLFiles():
inputtxt.delete(1.0, END)
for file in tdl_list:
subfolder = "templates"
file_path = os.path.join(subfolder, str(file))
with open(str(file_path), "r") as file:
inputtxt.insert(END,str(file.read()+ '\n')) #A NEWLINE IS INSERTED FOR EVERY NEW LOADED FILE
def preSave():
info_window = tk.Toplevel()
info_window.grab_set()
info_window.title("Save the model")
info_window.geometry("600x120")
info_label = tk.Label(info_window, text="Please, select a name for the file", font= ('arial',18) )
info_label.grid(row=0, column=0)
boolean = tk.IntVar()
radioButtonSDL = ttk.Radiobutton(
info_window,
text='.sdl',
value=1, #TODO right now I can select both radiobuttons, while only one just have to do so
variable= boolean
)
radioButtonTDL = ttk.Radiobutton(
info_window,
text='.tdl',
value=2,
variable= boolean
)
radioButtonSDL.grid(row=1, column= 1)
radioButtonTDL.grid(row=1, column=2)
radioButtonSDL.invoke()
saveTextBox= tk.Text(info_window, height= 1, width= 18)
saveTextBox.grid(row=1, column=0)
close_button = tk.Button(info_window, text="Save", command= lambda : saveFile(info_window, saveTextBox.get(0.0,END).strip(), boolean) )
close_button.grid(row=1, column=3)
def saveFile(info_window, filename, boolean):
info_window.destroy()
info_window.update()
if (boolean.get() == 1):
filename = filename + ".sdl".strip()
else:
filename = filename + ".tdl".strip()
try:
subfolder = "saved_models"
file_path = os.path.join(subfolder, filename)
text_file = open(file_path, "w")
text_file.write(inputtxt.get("1.0", "end-1c").strip()) #the file is saved in the current folder
text_file.close()
inputtxt.delete(1.0, END) #textbox is now blank
msgbox.showinfo("Information", "file " +filename+" has been saved!")
except Exception as e:
msgbox.showerror("error", "an error occurred!"+str(e))
refreshListBox(self.path)
def goHome():
reset()
controller.show_mainPage()
def wipeText():
inputtxt.delete(1, END)
# FRAME DECLARATION & GRID
#frame placed in the top side of the window
topFrame = tk.Frame(self, width = 200 , height= 30)
topFrame.grid(row = 0, column= 0 )
#frame placed in the centre of the window
centerFrame = tk.Frame(self, highlightbackground= 'black', width = 200 , height= 200)
centerFrame.grid(row = 1, column= 0 )
#frame placed in the right side of the window
rightFrame = tk.Frame(self, width = 100 , height= 100, padx= 50, pady= 50)
rightFrame.grid(row = 1, column= 1)
#frame palced at the bottom of the window
bottomFrame = tk.Frame(self, width = 200 , height= 200)
bottomFrame.grid(row = 8, column= 0 )
#frame used inside RightFrame
buttonsFrame = tk.Frame(rightFrame)
buttonsFrame.grid(row=2, column=0)
#LABEL & BUTTON DECLARATION & GRID
resetButton = ttk.Button(bottomFrame, text="RESET", command= wipeText , style= 'CustomButton.TButton') #function to redet the whole design window, has not be implemented yet // TODO
resetButton.grid(column=0, row=0, padx = 3, pady = 3)
#model button should load the file
#button MODEL used to retrieve .tdl or .sdl files
modelButton = ttk.Button(bottomFrame, text="SAVE", command= preSave, style= 'CustomButton.TButton' ) #function to load a file from local storage, for now just a single one
modelButton.grid(column=1, row=0, padx = 3, pady = 3)
deleteButton= ttk.Button(buttonsFrame, text="delete", command= delete_item , style= 'CustomButton.TButton') #not sure if this is essential, for now I'll just leave it here
deleteButton.grid(column=0, row=0, padx = 8, pady = 3) #it does the same action as Service/Target Template, for now.
loadButton = ttk.Button(buttonsFrame, text="load", command= showFile ,style= 'CustomButton.TButton')
loadButton.grid(column=1, row=0, padx = 8, pady = 3)
label = ttk.Label(centerFrame, text ="Stochastic Policy Design Time", font = LARGEFONT)
label.grid(row = 0, column = 0)
self.listBox = tk.Listbox(rightFrame, width= 30, height= 25, font= ('arial',14))
#select the file from os explorer and return its directory as string type, PROBABLY NOT USEFUL, BUT I KEEP IT HERE FOR NOW
#global file
#file = filedialog.askdirectory(
#title='Select the file', #name of the tab
#initialdir="C:/Users/anton/OneDrive/Documenti/Software Engineering/Fellowship/Adaptive", #initial shown directory
# )
#self.path = file
#print(str(self.path))
#refreshListBox("./templates")
self.listBox.grid(row= 0, column= 0)
loadedFileLabel = ttk.Label(rightFrame, text='here is the list of all loaded files: \n')
#loadedFileLabel.grid(row = 1, column= 5, padx= 10)
startPageButton = ttk.Button(bottomFrame, text ="Home", command = goHome , style= 'CustomButton.TButton')
startPageButton.grid(row = 0, column = 2, padx = 3, pady = 3)
modelServiceButton = ttk.Button(topFrame, text = " Service Template ", command= loadSDLFiles, style= 'CustomButton.TButton')
modelServiceButton.grid(row = 1, column = 0, pady= 5)
modelTargetButton = ttk.Button(topFrame, text = " Target Template ", command= loadTDLFiles, style= 'CustomButton.TButton')
modelTargetButton.grid(row = 1, column = 1, pady= 5)
inputtxt = tk.Text(centerFrame,
height = 38,
width = 115,
)
inputtxt.grid(row = 1 , column= 0 )
global dynamicLabel
dynamicLabel = ttk.Label(rightFrame, text = "select some files" ) #this label shows the directory of the selected file