Source code for interface.agents

"""
Frontend Agent Creation Page File for AGE-ABM Visual Interface
@Author Meghan Ireland
@Co-Authors Max Hall and Matthew Fleischman

"""

import tkinter as tk
from tkinter import ttk
import customtkinter

[docs] class Agents: """Defines widgets and functionality of agents page. """ def __init__(self, ec_app_ui): ### Table for added components ### self.ec_app_ui = ec_app_ui self.id7 = 0 self.ad_comp_frame = customtkinter.CTkFrame(self.ec_app_ui.page_frame, fg_color=self.ec_app_ui.tbl_colour, height = 200, width = self.ec_app_ui.window_width/5) self.vertical_scroll3 = tk.Scrollbar(self.ad_comp_frame) self.ad_comp_tbl = ttk.Treeview(self.ad_comp_frame,padding = 5,height=4 , yscrollcommand=self.vertical_scroll3.set) self.ad_comp_tbl['columns'] = 'Name' self.ad_comp_tbl.column("#0", width=0, stretch='NO') self.ad_comp_tbl.column("Name",anchor='center', width=250) self.ad_comp_tbl.heading("#0",text="",anchor='center') self.temp_comp_arr = [] self.comp_number = 0 self.temp_components_not_added = [] self.arr_agent_lvl_attributes = [] self.arr_all_attributes = [] self.ad_comp_tbl.heading("Name",text="Added Components",anchor='center') self.ad_comp_tbl.bind("<<TreeviewSelect>>", self.display_selected_component) ### Frames ### self.frame_small = tk.Frame(master = self.ec_app_ui.page_frame, bg=self.ec_app_ui.frame_colour , height = self.ec_app_ui.button_height , width = 3*self.ec_app_ui.button_width//2) ### Titles ### self.label_agents_title = tk.Label(self.ec_app_ui.title_frame, text="Agents" , font=self.ec_app_ui.font1, fg = self.ec_app_ui.label_text_colour , bg=self.ec_app_ui.title_colour) self.label_agents_add_new = tk.Label(self.ec_app_ui.title_frame, text="Add Agent", font=self.ec_app_ui.font1, fg = self.ec_app_ui.label_text_colour , bg=self.ec_app_ui.title_colour) self.label_agents_title_edit = tk.Label(self.ec_app_ui.title_frame, text="Edit Agent", font=self.ec_app_ui.font1, fg = self.ec_app_ui.label_text_colour , bg=self.ec_app_ui.title_colour) self.label_agents_set_alevel_attr_title = tk.Label(self.ec_app_ui.title_frame, text="Set Agent Level Attributes", font=self.ec_app_ui.font1 , fg = self.ec_app_ui.label_text_colour, bg=self.ec_app_ui.title_colour) ### Labels ### self.label_agents_add_component = tk.Label(self.ec_app_ui.page_frame, text="Add Component" , font=self.ec_app_ui.font3_underlined, fg = self.ec_app_ui.label_text_colour , bg=self.ec_app_ui.frame_colour) self.label_agents_select_component = tk.Label(self.ec_app_ui.page_frame , text="Select Components to add:", font=self.ec_app_ui.font2, justify="left" , bg=self.ec_app_ui.frame_colour, fg=self.ec_app_ui.label_text_colour) self.label_agents_name = tk.Label(self.ec_app_ui.page_frame, text="Agent Name:" , font=self.ec_app_ui.font2, justify="left",bg=self.ec_app_ui.frame_colour , fg=self.ec_app_ui.label_text_colour) self.label_agents_description = tk.Label(self.ec_app_ui.page_frame, text="Description:" , font=self.ec_app_ui.font2, justify="left", bg=self.ec_app_ui.frame_colour , fg=self.ec_app_ui.label_text_colour) self.label_agents_xco = tk.Label(self.ec_app_ui.page_frame, text="Initial x-Coordinate (refer to width as WIDTH):" , font=self.ec_app_ui.font2,bg=self.ec_app_ui.frame_colour , fg=self.ec_app_ui.label_text_colour) self.label_agents_yco = tk.Label(self.ec_app_ui.page_frame, text="Initial y-Coordinate (refer to height as HEIGHT):" , font=self.ec_app_ui.font2,bg=self.ec_app_ui.frame_colour , fg=self.ec_app_ui.label_text_colour) self.label_agents_alevel_attributes = tk.Label(self.ec_app_ui.page_frame , text="Currently Setting:", font=self.ec_app_ui.font2, bg=self.ec_app_ui.frame_colour , fg=self.ec_app_ui.label_text_colour) ### Entry ### self.entry_agents_name = customtkinter.CTkEntry(self.ec_app_ui.page_frame , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , fg_color = self.ec_app_ui.entry_colour, width = self.ec_app_ui.entry_width , text_color = self.ec_app_ui.entry_text_colour) self.entry_agents_description = customtkinter.CTkEntry(self.ec_app_ui.page_frame , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , fg_color = self.ec_app_ui.entry_colour,width = self.ec_app_ui.entry_width , text_color = self.ec_app_ui.entry_text_colour) self.entry_agents_xco = customtkinter.CTkEntry(self.ec_app_ui.page_frame , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , fg_color = self.ec_app_ui.entry_colour,width = self.ec_app_ui.entry_width , text_color = self.ec_app_ui.entry_text_colour) self.entry_agents_yco = customtkinter.CTkEntry(self.ec_app_ui.page_frame , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , fg_color = self.ec_app_ui.entry_colour,width = self.ec_app_ui.entry_width , text_color = self.ec_app_ui.entry_text_colour) self.entry_agents_attr_val = customtkinter.CTkTextbox(self.ec_app_ui.page_frame , font = self.ec_app_ui.font2, height=4*self.ec_app_ui.entry_height , fg_color = self.ec_app_ui.entry_colour,width = self.ec_app_ui.entry_width , text_color = self.ec_app_ui.entry_text_colour) ### Dropdown ### self.dropdown_agents_components = customtkinter.CTkOptionMenu(self.ec_app_ui.page_frame , values = [], fg_color = self.ec_app_ui.dropdown_menu_color , button_color=self.ec_app_ui.comp_btn_colour , button_hover_color= self.ec_app_ui.highlight_colour , height = self.ec_app_ui.entry_height, width=self.ec_app_ui.entry_width , font = self.ec_app_ui.font2, command = self.btn_agents_add_component) self.dropdown_agents_components.set("Select Component") self.dropdown_agents_attributes = customtkinter.CTkOptionMenu(self.ec_app_ui.page_frame , values = [], fg_color = self.ec_app_ui.dropdown_menu_color , button_color=self.ec_app_ui.comp_btn_colour, command = self.on_selected_attr , button_hover_color= self.ec_app_ui.highlight_colour , height = self.ec_app_ui.entry_height, width=self.ec_app_ui.entry_width , font = self.ec_app_ui.font2) self.dropdown_agents_attributes.set("Select Attribute") ### Buttons ### self.button_agents_add = customtkinter.CTkButton(self.frame_small , hover_color=self.ec_app_ui.highlight_colour, text="Add" , command=self.btn_agents_add, fg_color=self.ec_app_ui.comp_btn_colour , font = self.ec_app_ui.font2, height=self.ec_app_ui.button_height , width=self.ec_app_ui.button_width/2) self.button_agents_edit = customtkinter.CTkButton(self.frame_small , hover_color=self.ec_app_ui.highlight_colour, text="Edit" , command=self.btn_agents_edit, fg_color=self.ec_app_ui.comp_btn_colour , font = self.ec_app_ui.font2, height=self.ec_app_ui.button_height , width=self.ec_app_ui.button_width/2) self.button_agents_remove = customtkinter.CTkButton(self.frame_small , hover_color=self.ec_app_ui.highlight_colour, text="Remove" , command=self.btn_agents_remove, fg_color=self.ec_app_ui.comp_btn_colour , font = self.ec_app_ui.font2, height=self.ec_app_ui.button_height , width=self.ec_app_ui.button_width/2) self.button_agents_save = customtkinter.CTkButton(self.ec_app_ui.page_frame , hover_color=self.ec_app_ui.highlight_colour, text="✔️ Save Agent" , command=self.btn_agents_save, fg_color=self.ec_app_ui.comp_btn_colour , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , width= self.ec_app_ui.entry_width) self.button_agents_save_edited = customtkinter.CTkButton(self.ec_app_ui.page_frame , hover_color=self.ec_app_ui.highlight_colour, text="✔️ Save Agent" , command=self.btn_agents_save_edited, fg_color=self.ec_app_ui.comp_btn_colour , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , width= self.ec_app_ui.entry_width) self.button_agents_save_attr_val = customtkinter.CTkButton(self.ec_app_ui.page_frame , hover_color=self.ec_app_ui.highlight_colour, text="Save Value" , command=self.btn_agents_add_alevel_attr, fg_color=self.ec_app_ui.comp_btn_colour , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , width=self.ec_app_ui.entry_width) self.button_agents_insert_attr = customtkinter.CTkButton(self.ec_app_ui.page_frame , hover_color=self.ec_app_ui.highlight_colour, text="Insert" , command=self.btn_agents_insert_attr, fg_color=self.ec_app_ui.comp_btn_colour , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , width=self.ec_app_ui.entry_width) self.button_agents_remove_component = customtkinter.CTkButton(self.ec_app_ui.page_frame , hover_color=self.ec_app_ui.highlight_colour, text="Remove Component" , command=self.btn_agents_remove_component, fg_color=self.ec_app_ui.comp_btn_colour , font = self.ec_app_ui.font2, height=self.ec_app_ui.entry_height , width=self.ec_app_ui.entry_width) ### Table for Agents ### self.agent_frame = customtkinter.CTkFrame(self.ec_app_ui.page_frame , fg_color=self.ec_app_ui.tbl_colour , height = 300, width = 580) self.vertical_scroll2 = tk.Scrollbar(self.agent_frame) self.agent_tbl = ttk.Treeview(self.agent_frame,padding = 5,height = 8 , yscrollcommand=self.vertical_scroll2.set) # The code below sets up the table for agents self.agent_tbl['columns'] = ('Agent', 'Description') self.agent_tbl.column("#0", width=0, stretch='NO') self.agent_tbl.column("Agent",anchor='center', width=190) self.agent_tbl.column("Description",anchor='center',width=380) self.agent_tbl.heading("#0",text="",anchor='center') self.agent_tbl.heading("Agent",text="Agent",anchor='center') self.agent_tbl.heading("Description",text="Description",anchor='center') self.agent_tbl.bind("<<TreeviewSelect>>", self.display_selected_agent) self.selected_agent = "" ### Table for components and attributes ### self.ad_comp_attr_frame = customtkinter.CTkFrame(self.ec_app_ui.page_frame , fg_color=self.ec_app_ui.tbl_colour , height = 200, width = self.ec_app_ui.window_width/6) self.vertical_scroll = tk.Scrollbar(self.ad_comp_attr_frame) self.ad_comp_attr_tbl = ttk.Treeview(self.ad_comp_attr_frame,padding = 5,height=4 , yscrollcommand=self.vertical_scroll.set) self.ad_comp_attr_tbl['columns'] = ('Component', 'Attribute') self.ad_comp_attr_tbl.column("#0", width=0, stretch='NO') self.ad_comp_attr_tbl.column("Component",anchor='center', width=200) self.ad_comp_attr_tbl.column("Attribute",anchor='center',width=200) self.ad_comp_attr_tbl.heading("#0",text="",anchor='center') self.ad_comp_attr_tbl.heading("Component",text="Component",anchor='center') self.ad_comp_attr_tbl.heading("Attribute",text="Attribute",anchor='center') self.ad_comp_attr_tbl.bind("<<TreeviewSelect>>", self.display_selected_attribute) self.clicked_attribute = "" # Holds the current attribute self.clicked_component = "" # Holds the current component self.current_agent = "" # Holds the current agent self.selected_component = ""
[docs] def clear_add_agent(self): """Clears the add agent page """ self.label_agents_add_new.grid_forget() self.label_agents_description.grid_forget() self.label_agents_name.grid_forget() self.label_agents_select_component.grid_forget() self.label_agents_add_component.grid_forget() self.label_agents_xco.grid_forget() self.label_agents_yco.grid_forget() self.label_agents_alevel_attributes.grid_forget() self.label_agents_set_alevel_attr_title.grid_forget() self.entry_agents_name.grid_forget() self.entry_agents_xco.grid_forget() self.entry_agents_yco.grid_forget() self.entry_agents_description.grid_forget() self.entry_agents_attr_val.grid_forget() self.dropdown_agents_components.grid_forget() self.dropdown_agents_attributes.grid_forget() self.ad_comp_frame.grid_forget() self.ad_comp_tbl.pack_forget() self.ad_comp_attr_frame.grid_forget() self.ad_comp_attr_tbl.pack_forget() self.button_agents_save_attr_val.grid_forget() self.button_agents_insert_attr.grid_forget() self.button_agents_save.grid_forget() self.button_agents_save_edited.grid_forget() self.button_agents_remove_component.grid_forget() self.frame_small.grid_forget() self.label_agents_title_edit.grid_forget() self.label_agents_set_alevel_attr_title.grid_forget()
[docs] def clear_agents(self): """Clears the agent page """ self.agent_frame.grid_forget() self.agent_tbl.pack_forget() self.button_agents_add.grid_forget() self.button_agents_edit.grid_forget() self.button_agents_remove.grid_forget() self.label_agents_title.grid_forget()
[docs] def agents_tab_btn(self): """Called when user clicks agent page """ self.temp_comp_arr = [] self.selected_agent = "" self.selected_component = "" self.ec_app_ui.clear_all() self.entry_agents_name.configure(state = "normal") id2 = 0 self.label_agents_title.grid(row = 0, column = 0) # Add all agents to the agent table from the backend for agent in self.agent_tbl.get_children(): self.agent_tbl.delete(agent) agent_names = self.ec_app_ui.new_model.get_list_agents() for agent_name in agent_names: agent = self.ec_app_ui.new_model.get_agent(agent_name) self.agent_tbl.insert(parent='',index="end",iid=id2,text='' ,values=(agent_name, agent.get_description())) id2+=1 ### Adds widgets to Agent tab ### ### Table ### self.agent_frame.grid(row = 0,columnspan = 3,column = 0,pady = (40,5)) self.vertical_scroll2.pack(side="right", fill="y", padx=(0,5), pady=5) self.agent_tbl.pack(fill = "x", padx = (5,0), pady = 5) ### Add, edit, remove buttons ### self.frame_small.grid(row=2, column = 0, columnspan=3,pady = 5) self.button_agents_add.grid(row = 0,column = 0,padx = 5) self.button_agents_edit.grid(row = 0, column = 1,padx = 5) self.button_agents_remove.grid(row = 0, column = 2,padx = 5)
[docs] def btn_agents_add(self): """Displays options for users to enter a new agent """ self.ec_app_ui.clear_all() self.entry_agents_name.delete(0, 'end') self.entry_agents_description.delete(0, 'end') self.ec_app_ui.clear_all() for comp in self.ad_comp_tbl.get_children(): self.ad_comp_tbl.delete(comp) self.dropdown_agents_components.set('') self.temp_components_not_added = self.ec_app_ui.new_model.get_list_components() self.dropdown_agents_components.configure(self, values = self.temp_components_not_added) self.label_agents_add_new.grid(row = 0, column = 0) self.label_agents_name.grid(row = 0, column = 0,padx = 150,pady = 5,sticky="W") self.entry_agents_name.grid(row = 0, column = 1,pady = 5) self.label_agents_description.grid(row = 1, column = 0,padx = 150,pady = 5,sticky="W") self.entry_agents_description.grid(row = 1, column = 1,pady = 5) if self.ec_app_ui.new_model.get_model_details().get_gridworld(): self.entry_agents_xco.delete(0,'end') self.entry_agents_yco.delete(0,'end') self.label_agents_xco.grid(row = 2, column = 0,padx = 150, pady = 5, sticky="W") self.label_agents_yco.grid(row = 3, column = 0,padx = 150, pady = 5, sticky="W") self.entry_agents_xco.grid(row = 2, column = 1) self.entry_agents_yco.grid(row = 3, column = 1,pady = 5) self.comp_number = 1 self.label_agents_add_component.config(text="Add Component " + str(self.comp_number)) self.label_agents_add_component.grid(row = 4, column = 0,padx = 150,pady = 5,sticky="W") self.label_agents_select_component.grid(row = 5, column = 0,padx = 150,pady = 5,sticky="W") self.dropdown_agents_components.grid(row = 5, column = 1,pady = 5,padx=(0,5)) self.button_agents_save.grid(row = 6, column = 1,pady = 5) ### Table ### self.ad_comp_frame.grid(row = 7, column = 0, pady = 5) self.vertical_scroll3.pack(side="right", fill="y", padx=(0,5), pady=5) self.ad_comp_tbl.pack(fill = "x", padx = (5,0), pady = 5) self.button_agents_remove_component.grid(row = 8, column = 0, pady = 5)
[docs] def btn_agents_save(self): """Saves agent """ agent_name = self.entry_agents_name.get() self.current_agent = agent_name agent_description = self.entry_agents_description.get() if self.ec_app_ui.new_model.get_model_details().get_gridworld(): self.ec_app_ui.new_model.add_agent(agent_name, agent_description, self.temp_comp_arr , [self.entry_agents_xco.get(), self.entry_agents_yco.get()]) else: self.ec_app_ui.new_model.add_agent(agent_name, agent_description, self.temp_comp_arr) for agent_name in self.ad_comp_attr_tbl.get_children(): self.ad_comp_attr_tbl.delete(agent_name) self.id7 = 0 ### Check whether there are any agent level defined attributes ### has_agent_level = False self.arr_all_attributes = [] self.arr_agent_lvl_attributes = [] list_comps = self.temp_comp_arr for component in list_comps: for attr in self.ec_app_ui.new_model.get_list_attributes(component): if self.ec_app_ui.new_model.get_attribute(component, attr).get_define_level() == "agent_lvl": has_agent_level = True self.arr_agent_lvl_attributes.append(component+"."+attr) else: self.arr_all_attributes.append([component, attr]) if not has_agent_level: # If there are no agent level attributes it goes back to the table page self.agents_tab_btn() else: # Displays options for adding agent level attributes self.clear_add_agent() self.label_agents_set_alevel_attr_title.grid(row = 0, column = 0) self.dropdown_agents_attributes.configure(values = self.arr_agent_lvl_attributes) self.dropdown_agents_attributes.set("Select Attribute") for attr in self.ad_comp_attr_tbl.get_children(): self.ad_comp_attr_tbl.delete(attr) ### Fill the components attribute table: ### id_attributes = 0 for val in self.arr_all_attributes: self.ad_comp_attr_tbl.insert(parent='', index="end" , iid=id_attributes, text='', values=val) id_attributes += 1 self.temp_comp_arr = [] ### Table of attributes ### self.ad_comp_attr_frame.grid(row = 0, column = 0, rowspan = 6, pady = 5, padx = (40,5)) self.vertical_scroll.pack(side="right", fill="y", padx=(0,5), pady=5) self.ad_comp_attr_tbl.pack(fill = "x", padx = (5,0), pady = 5) ### Insert button ### self.button_agents_insert_attr.grid(row = 6, column = 0) ### Title ### self.label_agents_alevel_attributes.grid(row = 0, column = 1, sticky="W", pady = 5, padx = 5) ### Drop down ### self.dropdown_agents_attributes.grid(row = 0, column = 2, sticky="W", pady = 5, padx = 5)
[docs] def btn_agents_save_edited(self): """Saves agent """ agent_name = self.entry_agents_name.get() self.current_agent = agent_name agent_description = self.entry_agents_description.get() if self.ec_app_ui.new_model.get_model_details().get_gridworld(): self.ec_app_ui.new_model.edit_agent(agent_name, agent_description, self.temp_comp_arr , [self.entry_agents_xco.get(), self.entry_agents_yco.get()]) else: self.ec_app_ui.new_model.edit_agent(agent_name, agent_description, self.temp_comp_arr) for agent_name in self.ad_comp_attr_tbl.get_children(): self.ad_comp_attr_tbl.delete(agent_name) ### Check whether there are any agent level defined attributes ### has_agent_level = False self.arr_all_attributes = [] self.arr_agent_lvl_attributes = [] list_comps = self.temp_comp_arr for component in list_comps: for attr in self.ec_app_ui.new_model.get_list_attributes(component): if self.ec_app_ui.new_model.get_attribute(component, attr).get_define_level() == "agent_lvl": has_agent_level = True self.arr_agent_lvl_attributes.append(component+"."+attr) else: self.arr_all_attributes.append([component, attr]) if not has_agent_level: # If there are no agent level attributes it goes back to the table page self.agents_tab_btn() else: # Displays options for adding agent level attributes self.label_agents_set_alevel_attr_title.grid(row = 0, column = 0) self.clear_add_agent() self.dropdown_agents_attributes.configure(values = self.arr_agent_lvl_attributes) self.dropdown_agents_attributes.set("Select Attribute") for attr in self.ad_comp_attr_tbl.get_children(): self.ad_comp_attr_tbl.delete(attr) ### Fill the components attribute table: ### id_attributes = 0 for val in self.arr_all_attributes: self.ad_comp_attr_tbl.insert(parent='',index="end", iid=id_attributes, text='', values=val) id_attributes += 1 self.temp_comp_arr = [] ### Table of attributes ### self.ad_comp_attr_frame.grid(row = 0, column = 0, rowspan = 6, pady = 5, padx = (40,5)) self.vertical_scroll.pack(side="right", fill="y", padx=(0,5), pady=5) self.ad_comp_attr_tbl.pack(fill = "x", padx = (5,0), pady = 5) ### Insert button ### self.button_agents_insert_attr.grid(row = 6, column = 0) ### Title ### self.label_agents_alevel_attributes.grid(row = 0, column = 1, sticky="W", pady = 5, padx = 5) ### Drop down ### self.dropdown_agents_attributes.grid(row = 0, column = 2, sticky="W", pady = 5, padx = 5)
[docs] def btn_agents_add_component(self, *args): """Adds component to agent """ new_comp = self.dropdown_agents_components.get() self.temp_comp_arr.append(new_comp) self.update_comp_tbl() self.comp_number+=1 self.label_agents_add_component.config(text="Add Component "+str(self.comp_number)) self.temp_components_not_added.remove(new_comp) self.dropdown_agents_components.set('Select components') self.dropdown_agents_components.configure(self, values=self.temp_components_not_added)
[docs] def btn_agents_edit(self): """Allows users to edit the selected agent """ if self.selected_agent != "": self.ec_app_ui.clear_all() self.label_agents_title_edit.grid(row = 0, column = 0) model_agent = self.ec_app_ui.new_model.get_agent(self.selected_agent) ### Agent Name ### self.entry_agents_name.delete(0, 'end') self.entry_agents_name.insert(0, self.selected_agent) self.entry_agents_name.configure(state = "disabled") ### Description ### self.entry_agents_description.delete(0, 'end') self.entry_agents_description.insert(0,model_agent.get_description()) ### Update components table ### self.temp_comp_arr = model_agent.get_given_components() self.update_comp_tbl() self.dropdown_agents_components.set('') ### Get components and remove from list ### self.temp_components_not_added = self.ec_app_ui.new_model.get_list_components() for i in self.temp_comp_arr: self.temp_components_not_added.remove(i) self.dropdown_agents_components.configure(self, values = self.temp_components_not_added) ### Name ### self.label_agents_name.grid(row = 0, column = 0,padx = 150,pady = 5,sticky="W") self.entry_agents_name.grid(row = 0, column = 1,pady = 5) ### Description ### self.label_agents_description.grid(row = 1, column = 0,padx = 150,pady = 5,sticky="W") self.entry_agents_description.grid(row = 1, column = 1,pady = 5) ### Small title ### self.label_agents_add_component.config(text="Add a Component") ### x and y coordinates ### if self.ec_app_ui.new_model.get_model_details().get_gridworld(): self.entry_agents_xco.delete(0,'end') self.entry_agents_yco.delete(0,'end') coords = model_agent.get_init_coordinates() self.entry_agents_xco.insert(0,coords[0]) self.entry_agents_yco.insert(0,coords[1]) self.label_agents_xco.grid(row = 2, column = 0,padx = 150, pady = 5, sticky="W") self.label_agents_yco.grid(row = 3, column = 0,padx = 150, pady = 5, sticky="W") self.entry_agents_xco.grid(row = 2, column = 1) self.entry_agents_yco.grid(row = 3, column = 1,pady = 5) ### Add component ### self.label_agents_add_component.grid(row = 4, column = 0,padx = 150,pady = 5,sticky="W") self.label_agents_select_component.grid(row = 5, column = 0,padx = 150,pady = 5,sticky="W") self.dropdown_agents_components.grid(row = 5, column = 1,pady = 5,padx=(0,5)) ### Save agent ### self.button_agents_save_edited.grid(row = 6, column = 1,pady = 5) ### Component frame and table ### self.ad_comp_frame.grid(row = 7, column = 0, columnspan = 1,pady = 5) self.vertical_scroll3.pack(side="right", fill="y", padx=(0,5), pady=5) self.ad_comp_tbl.pack(fill = "x", padx = (5,0), pady = 5) self.button_agents_remove_component.grid(row = 8, column = 0, pady = 5)
[docs] def btn_agents_add_alevel_attr(self): """Allows Users to add agent level attributes, saves them to backend in required format """ combined = self.dropdown_agents_attributes.get().split(".") component = combined[0] attribute = combined[1] value = self.entry_agents_attr_val.get("1.0", 'end-1c') for combination in self.arr_all_attributes: specific_string = f"{combination[0]}${combination[1]}" value = value.replace(specific_string , f"{self.current_agent}Agent[{combination[0]}Component].{combination[1]}") self.ec_app_ui.new_model.add_agent_lvl_default_value(self.current_agent, component , attribute, value) self.arr_agent_lvl_attributes.remove(self.dropdown_agents_attributes.get()) self.dropdown_agents_attributes.configure(values = self.arr_agent_lvl_attributes) self.dropdown_agents_attributes.set("Select Attribute") self.entry_agents_attr_val.grid_forget() self.button_agents_save_attr_val.grid_forget() if len(self.arr_agent_lvl_attributes) == 0: self.agents_tab_btn()
[docs] def btn_agents_remove(self): """Allows a user to delete selected component. """ self.ec_app_ui.new_model.remove_agent(self.selected_agent) self.agents_tab_btn()
[docs] def on_selected_attr(self, *args): """When user clicks dropdown """ if not self.dropdown_agents_attributes.get() == "Select Attribute": ### Entry ### self.entry_agents_attr_val.delete("1.0", 'end-1c') if self.selected_agent!="": attribute = self.ec_app_ui.new_model.get_attribute(self.dropdown_agents_attributes.get().split(".")[0], self.dropdown_agents_attributes.get().split(".")[1]).get_default_value()[self.selected_agent] self.entry_agents_attr_val.insert("1.0", attribute) self.entry_agents_attr_val.grid(row = 1, column = 2, sticky="W", pady = 5, padx = 5) ### Save Button ### self.button_agents_save_attr_val.grid(row = 2, column = 2, sticky="W", pady = 5 , padx = 5)
[docs] def btn_agents_insert_attr(self): """Adds the specified attribute to the attribute value """ self.entry_agents_attr_val.insert(tk.END, self.clicked_component+"$"+self.clicked_attribute)
[docs] def display_selected_attribute(self, *args): """Saves the attribute the user clicked on. """ self.clicked_attribute = "" self.clicked_component = "" selected_items = self.ad_comp_attr_tbl.selection() if selected_items: selected_item = self.ad_comp_attr_tbl.selection()[0] self.clicked_attribute = self.ad_comp_attr_tbl.item(selected_item)['values'][1] self.clicked_component = self.ad_comp_attr_tbl.item(selected_item)['values'][0]
[docs] def display_selected_agent(self, *args): """Saves the agent the user clicked on. """ self.selected_agent = "" selected_items = self.agent_tbl.selection() if selected_items: selected_item = self.agent_tbl.selection()[0] self.selected_agent = self.agent_tbl.item(selected_item)['values'][0]
[docs] def update_comp_tbl(self): """Updates the small table of components """ id_comp = 0 for i in self.ad_comp_tbl.get_children(): self.ad_comp_tbl.delete(i) for component in self.temp_comp_arr: self.ad_comp_tbl.insert(parent='',index='end',iid=id_comp,text='',values=(component,)) id_comp += 1
[docs] def display_selected_component(self, *args): """Gets value user selects from the table """ self.selected_component = "" selected_items = self.ad_comp_tbl.selection() if selected_items: selected_item = self.ad_comp_tbl.selection()[0] self.selected_component = self.ad_comp_tbl.item(selected_item)['values'][0]
[docs] def btn_agents_remove_component(self): """Removes the component from added components """ if self.selected_component != "": self.temp_comp_arr.remove(self.selected_component) self.temp_components_not_added.append(self.selected_component) self.selected_component = "" self.dropdown_agents_components.configure(values = self.temp_components_not_added) self.update_comp_tbl() self.comp_number-=1 self.label_agents_add_component.config(text="Add Component "+str(self.comp_number))