Reputation: 1
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.
Upvotes: 0
Views: 41
Reputation: 47085
Inside new_folder()
:
add_image()
to add the new folder labelfunctions.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.
Upvotes: 0