I created a dictionary of python tkinter frames and when I try to loop through the frame to call a functions which updates the labels in each frame, only the last frame gets updated and rendered to the GUI window. This happens regardless of the number of frames in the dictionary. For example, if I have 2 frames, only the 2nd gets rendered, if I have 4 frames, only the 4th gets rendered to the GUI. What might be wrong?
I have tried to stop the loop from updating all the frames, such that it tries to only update the first frame in the dictionary (the dictionary still contains other frames). I did this because I suspected that the loop might be “forgetting” the previous labels when it proceeds to the next frame in the next iteration. The problem still persists, such that the first frame does not get updated and rendered, and since the last frame was not reached in the for loop, that also was not updated.
I have attached the relevant snippet of the code.
def create_monitor_page(self, monitor_location, monitor_no, job_type):
command0 = "/path/to/dir " + monitor_location
result0 = subprocess.run(command0, shell=True, capture_output=True, text=True)
command1 = f"cat {monitor_location}/status_file"
result1 = subprocess.run(command1, shell=True, capture_output=True, text=True)
a = result1.stdout
Label(monitor_canvas_dict_frame[f"{job_type}{monitor_no}"], text = f"Job Location ---> {monitor_location}", font = ("Times New Roman", 10, "bold")).grid(column = 1, row = 1, sticky = "w")
Label(monitor_canvas_dict_frame[f"{job_type}{monitor_no}"], text = a, font = ("Times New Roman", 10, "bold")).grid(column = 1, row = 2, sticky = "w")
def refresh_monitor_info(self):
jobs_dir = self.job_directory_server
for jobs_type in os.listdir(jobs_dir):
for individual_job in os.listdir(f"{jobs_dir}/{jobs_type}"):
rm_other_files = f"rm {jobs_dir}/{jobs_type}/{individual_job}/builds/status_file"
subprocess.run(rm_other_files, shell=True, capture_output=True, text=True)
ls_command = f"ls {jobs_dir}/{jobs_type}/{individual_job}/builds"
execute_command = subprocess.run(ls_command, shell=True, capture_output=True, text=True)
job_name = execute_command.stdout
self.monitoring.create_monitor_page(f"{jobs_dir}/{jobs_type}/{individual_job}/builds/{job_name[:-1]}", int(re.search(r'd+', individual_job).group()), jobs_type)
file_counter = 0
for files in os.listdir(script_directory):
monitor_frames_dict[f"server{file_counter}"] = Frame(launcher_obj.notebook)
launcher_obj.notebook.add(monitor_frames_dict[f"server{file_counter}"], text=f"Monitor{file_counter}_server")
monitor_canvas_dict[f"server{file_counter}"] = Canvas(monitor_frames_dict[f"server{file_counter}"])
server_project_vertical_scrollbar = Scrollbar(monitor_frames_dict[f"server{file_counter}"], orient=VERTICAL,
command=monitor_canvas_dict[f"server{file_counter}"].yview)
server_project_horizontal_scrollbar = Scrollbar(monitor_frames_dict[f"server{file_counter}"], orient=HORIZONTAL,
command=monitor_canvas_dict[f"server{file_counter}"].xview)
monitor_canvas_dict[f"server{file_counter}"].grid(row=0, column=0, sticky="nsew")
monitor_canvas_dict[f"server{file_counter}"].bind("<Configure>",
lambda cc: monitor_canvas_dict[f"server{file_counter-1}"].config(width=cc.width, height=cc.height))
monitor_canvas_dict_frame[f"server{file_counter}"] = Frame(monitor_canvas_dict[f"server{file_counter}"])
monitor_canvas_dict_frame[f"server{file_counter}"].bind("<Configure>", lambda e: monitor_canvas_dict[f"server{file_counter-1}"].configure(scrollregion=monitor_canvas_dict[f"server{file_counter-1}"].bbox("all")))
monitor_canvas_dict[f"server{file_counter}"].create_window((0, 0), window=monitor_canvas_dict_frame[f"server{file_counter}"])
monitor_canvas_dict[f"server{file_counter}"].configure(yscrollcommand=server_project_vertical_scrollbar.set)
monitor_canvas_dict[f"server{file_counter}"].configure(xscrollcommand=server_project_horizontal_scrollbar.set)
server_project_horizontal_scrollbar.pack(side="bottom", fill="x")
server_project_vertical_scrollbar.pack(side="right", fill="y")
monitor_canvas_dict[f"server{file_counter}"].pack(side="left", fill="both", expand=True)
launcher_obj.notebook.select(launcher_obj.notebook.index(tkinter.END) - 1)
launcher_obj.main_treeview.insert("server_monitor", 'end', iid=f"Monitor{file_counter}_server", text=f"Monitor{file_counter}_server")
file_counter = file_counter + 1
5