0

I am trying to update the widget frame when I add a new folder to the users directory, but I am unsure how to implement that function. This is what I have so far..

How I add the current directory folders in my main..

This is within my main script

        def add_image(row, col, folder):
            img = ImageTk.PhotoImage(fold_img)
            self.image_refs.append(img)
            
            imageLabel = Label(canvasFrame, image=img)
            imageLabel.grid(row=row, column=col, padx=5)
            
            nameLabel = Label(canvasFrame, text=folder, font=("Arial", 8), wraplength=100)
            nameLabel.grid(row=row , column=col)
            
            imageLabel.bind("<Button-1>", lambda event, name=folder: click(name))
    
        def add_folders():
            row = 0
            col = 0
            for folder in functions.folders:
                #print(f"added {folder} at {row} , {col}")
                add_image(row, col, folder)
                col += 1
                if col == 4:
                    row+= 1
                    col = 0
        
        add_folders()

How I create a new folder in the users directory.., I want the widget frame to update after this finishes

This is within my functions script

def new_folder(event):
    global cleaner
    cwd = os.getcwd()
    print(f" current: {cwd}")
    desktop = os.path.expanduser("~/Desktop")
    os.chdir(desktop)
    cwd = os.getcwd()
    print(f"new: {cwd}")
    
    name = simpledialog.askstring(title="new folder", prompt="enter folder name: ")
    
    try:
        os.makedirs(name)
        print(f"created {name}")
    except FileExistsError:
        print(f"there is folder named: {name}")
    except PermissionError:
        print(f"Permission denied: Unable to create '{name}'.")
    except Exception as e:
        print(f"An error occurred: {e}")

I am still new to this and hopefully this question makes sense.

1
  • run add_folders() directly after os.makedirs(...) (and first add cwd / name to functions.folders). But you would keep images or labels in global list to check if you already have this image(s) in widget - or delete all labels and create all again.. Commented Feb 27 at 11:45

1 Answer 1

0

Inside new_folder():

  • determine the row and column for the new folder label
  • call add_image() to add the new folder label
  • add the folder name to functions.folders (based on your code)
def new_folder(event):
    ...

    try:
        os.makedirs(name)
        # determine the row and column for the new folder label
        row, col = divmod(len(canvasFrame.grid_slaves()), 4)
        # add the folder label
        add_image(row, col, name)
        # add the folder name to functions.folders ???
        functions.folders.append(name)
        print(f"created {name}")
    except FileExistsError:
        ...
    ...

Note that:

  • you can use single label for showing the folder name and image instead of two labels

  • you have used same fold_img for all the folder labels inside add_image(), so you don't need to create new instance of ImageTk.PhotoImage() for each folder label, just create one globally and use it for all the folder labels.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.