使用 tkinter 标签中的 exec 从 python 中的列表打印文件路径
Print file paths from a list in python using exec in a tkinter label
我想在 tkinter 中显示文件名列表 window。该列表由文件路径组成。
我已经成功地将下面的代码与字符串项列表一起使用。我想知道我的列表 file_list
是否是我的问题,因为它的文件路径。也许文件路径在我的列表中显示不同,所以我需要以不同方式处理?
for y in range(len(file_list)):
exec(f'Label%d=Label(InfoWindow,text="%s")\nLabel%d.grid(row={y},
column=0, sticky=W)'%(y,file_list[y],y))
InfoWindow.mainloop()
我希望列表中的文件路径显示在我的 window 的每一行中,但事实并非如此。相反,我收到此错误消息:
exec(f'Label%d=Label(InfoWindow,text="%s")\nLabel%d.grid(row={y}, column=0, sticky=W)'%(y,file_list[y],y))
File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 12-13: malformed \N character escape
这将在 window 中为您创建 labels
,dict
也可以供以后参考。
name_dict = {}
for i in range(len(file_list)):
name_dict[i] = tk.Label(InfoWindow,text=file_list[i]).grid(row=i,column=0, sticky='w')
我想在 tkinter 中显示文件名列表 window。该列表由文件路径组成。
我已经成功地将下面的代码与字符串项列表一起使用。我想知道我的列表 file_list
是否是我的问题,因为它的文件路径。也许文件路径在我的列表中显示不同,所以我需要以不同方式处理?
for y in range(len(file_list)):
exec(f'Label%d=Label(InfoWindow,text="%s")\nLabel%d.grid(row={y},
column=0, sticky=W)'%(y,file_list[y],y))
InfoWindow.mainloop()
我希望列表中的文件路径显示在我的 window 的每一行中,但事实并非如此。相反,我收到此错误消息:
exec(f'Label%d=Label(InfoWindow,text="%s")\nLabel%d.grid(row={y}, column=0, sticky=W)'%(y,file_list[y],y))
File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 12-13: malformed \N character escape
这将在 window 中为您创建 labels
,dict
也可以供以后参考。
name_dict = {}
for i in range(len(file_list)):
name_dict[i] = tk.Label(InfoWindow,text=file_list[i]).grid(row=i,column=0, sticky='w')