【发布时间】: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 方法,但它不起作用
如何使每个“图标”变量的范围对类都是全局的?
【问题讨论】: