In this code there are two problems.
- The buttons created in
create_folder
are unclickable. - the image is not being inserted in the buttons.
Thanks in advance for any help.
import tkinter as tk
from os import listdir
from PIL import ImageTk,Image
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.canvas = tk.Canvas(self)
self.canvas.pack(side="left", fill="both", expand=1)
self.scrollbar = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
self.scrollbar.pack(side="right", fill="y")
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.bind("<Configure>", lambda event: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
self.frame = tk.Frame(self.canvas)
self.canvas.create_window((0, 0), window=self.frame, anchor="nw")
self.path = "D:"
self.val = 0
def create_folder(self):
lst = list(f for f in listdir("D:") if f[0] != "$")
p1 = ImageTk.PhotoImage(file="Folder.png")
for ech in lst:
button = tk.Button(master=self.frame, image=p1, text=ech, width=750, height=13, compound="left", anchor="w",
borderwidth=0.5)
button.pack(anchor="w")
if __name__ == "__main__":
root = tk.Tk()
root.geometry("600x600")
ex = explorer(root)
ex.create_folder(ex.path)
ex.pack(side="top", fill="both", expand=1)
root.mainloop()