Home > Mobile >  How to make images permanently display in treeview?
How to make images permanently display in treeview?

Time:11-02

I found a function that returns a bitmap icon with a given filepath. In addition to that, I have a function that makes a tree with files and folders on a given directory. I want my tree to be displaying either the name of the directory and its icon, but it only displays the name. I adopted an object-oriented approach for coding.

    def populate(self, path):
        if os.path.isdir(path):
            with os.scandir(path) as it:
                for entry in it:
                    if not entry.name.startswith('.') and entry.is_dir():
                        values1 = [time.asctime(time.localtime(os.path.getmtime(entry.path))), "Folder",
                                   os.path.getsize(entry.path)]
                        self.tree.insert("", 'end', text=entry.name, values=values1, open=False, image=self.folderimg)
                    if not entry.name.startswith('.') and entry.is_file():
                        icon_id = iconHelper.get_icon(path=entry.path, size="small")
                        icon = ImageTk.PhotoImage(icon_id)
                        values2 = [time.asctime(time.localtime(os.path.getmtime(entry.path))), "File",
                                   os.path.getsize(entry.path)]
                        self.tree.insert("", 'end', text=entry.name, values=values2, open=False, image=icon)
        else:
            try:
                os.startfile(path)
            except OSError:
                print("File can not be opened")

the "populate" function is in a class. I know that the scope of the icon variable is local. I tried a self.icon approach but it doesn't work

How can I do to make the scope of each "icon" variable global to the class?

CodePudding user response:

You can simply use an instance variable of type list to store those icon images:

def populate(self, path):
    if os.path.isdir(path):
        self.icons = [] # instance variable to store those icon images
        with os.scandir(path) as it:
            for entry in it:
                if not entry.name.startswith('.') and entry.is_dir():
                    values1 = [time.asctime(time.localtime(os.path.getmtime(entry.path))), "Folder",
                               os.path.getsize(entry.path)]
                    self.tree.insert("", 'end', text=entry.name, values=values1, open=False, image=self.folderimg)
                if not entry.name.startswith('.') and entry.is_file():
                    icon_id = iconHelper.get_icon(path=entry.path, size="small")
                    icon = ImageTk.PhotoImage(icon_id)
                    values2 = [time.asctime(time.localtime(os.path.getmtime(entry.path))), "File",
                               os.path.getsize(entry.path)]
                    self.tree.insert("", 'end', text=entry.name, values=values2, open=False, image=icon)
                    self.icons.append(icon) # save the reference of the icon image
    else:
        try:
            os.startfile(path)
        except OSError:
            print("File can not be opened")
  • Related