【问题标题】:How to make images permanently display in treeview?如何使图像永久显示在树视图中?
【发布时间】:2021-11-01 09:24:58
【问题描述】:

我找到了一个函数,它返回一个给定文件路径的位图图标。 除此之外,我还有一个函数可以在给定目录上创建一个包含文件和文件夹的树。 我希望我的树显示目录名称及其图标,但它只显示名称。我采用了面向对象的编码方法。

    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")

“填充”函数在一个类中。我知道icon 变量的范围是本地的。我尝试了self.icon 方法,但它不起作用

如何使每个“图标”变量的范围对类都是全局的?

【问题讨论】:

    标签: python tkinter treeview


    【解决方案1】:

    您可以简单地使用list 类型的实例变量来存储这些图标图像:

    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")
    

    【讨论】:

    • 您不想通过if not os.path.isdir(path): try:.... return 来降低主块的缩进级别,然后将其余代码保留在正常缩进级别(一个选项卡)
    • 谢谢!!我之前所做的是在插入树之前附加'''self.icons[]''' 不知道为什么它不起作用
    猜你喜欢
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多