0

In this code there are two problems.

  1. The buttons created in create_folder are unclickable.
  2. 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()
  • Please provide [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – JacksonPro Apr 06 '21 at 06:12
  • what do You mean they are unclickable? they work perfectly fine, I can click them and they show the clicking animation – Matiiss Apr 06 '21 at 07:40
  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – acw1668 Apr 07 '21 at 03:11

0 Answers0