简体   繁体   English

使用Tkinter标签中的exec从python中的列表中打印文件路径

[英]Print file paths from a list in python using exec in a tkinter label

I want to display a list of file names in a tkinter window. 我想在tkinter窗口中显示文件名列表。 the list consists of file paths. 该列表由文件路径组成。

I've successfully used the code below with a list of string items. 我已经成功地将以下代码与字符串项列表结合使用。 I'm wondering if the fact that my list file_list is my problem because its file paths. 我想知道我的列表file_list是否是我的问题,因为它的文件路径。 Maybe file paths show up differently in my list so i need to handle differently? 也许文件路径在列表中以不同的方式显示,所以我需要以不同的方式处理?

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

I am expecting a file path from the list to show up in a each row of my window but it's not. 我期望列表的文件路径显示在窗口的每一行中,但事实并非如此。 instead i get this error message: 相反,我收到此错误消息:

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

This will create labels for you in the window, also the dict can be used later for references. 这将在窗口中为您创建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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM