[Python] Updating Tkinter widget after function

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página