This is my first program after a bunch of small tutorials, and my file explorer works wonderfully. Did some custom icons, thumbnail for images, and I can scroll, multyselect stuff and browse with my icons. Originally it took 3.2 seconds to open/resize/blend-paste/display 81 pictures(100x100), and managed to bring it to 1.5 seconds. Multithreading will come soon :) I got rid of all my globals, to learn it the proper way, and now I want to understand classes more, I am happy that I made it work anyhow, but what is the correct way to initialize it in init?
I create an instance with:
folder_left = Folder(folder_left_settings.last_directory)
when I change the Folder:
folder_left.elements_create(values["-FOLDER-"])
folder_left.directory = values["-FOLDER-"]
folder_left_settings.last_directory = values["-FOLDER-"]
folder_left_settings.write_to_ini()
class Folder:
def __init__(self, directory):
self.directory = directory
self.elements_create(directory)
def elements_create(self, directory):
self.element_name = []
self.element_image_data = []
self.element_is_selected = []
self.element_is_type = []
self.list_of_indexes_marked_elements = []
if directory:
for file in os.listdir(directory):
if os.path.isdir(os.path.join(directory,file)):
self.element_name.append(file)
self.element_is_selected.append(False)
self.element_image_data.append(graphics_FolderIcon)
self.element_is_type.append("Icon")
if file.endswith((".gif", ".png", ".jpg")):
self.element_name.append(file)
self.element_is_selected.append(False)
self.element_is_type.append("Image")
try:
self.element_image_data.append(
image.convert_to_bytes(os.path.join(directory, file), (100, 100), fill_blanc_color=(90, 105, 121)))
except:
sg.popup(f"Error reading Image:{self.element_name}")
self.element_image_data.append(empty_image)
this tread Instance attribute attribute_name defined outside __init__ suggested to initialize them with None in init
I tried several combinations of:
def __init__(self, directory ***self.element_name=None***):
***self.element_name=None***
***self.element_name = []***
***self.element_name = element_name***
def elements_create(self, directory):
***self.element_name = []***
of course not all together, just the combinations(with and without)....nothing works, and my elements end up beeing None, or other exceptions. I know that I can do everything INSIDE ini directly and just overwrite create my instance if the folder changes, but I thought this would be better and proper. THX ALOT!!!
(I don't really understand the return 2nd answer with return as solution. It would make it pretty unreadable if I cannot append directly to the lists, since if understanding correctly, I would use placeholder objects in my method, so I can return all in one line. There must be a simple solution I don't see.)