如何为变量创建索引,以便将其用于tkinter?

问题描述 投票:-1回答:2

我遇到了一个无法显示图像的问题,因为“i”没有任何意义。 “i”是我用来确定替换字符的东西。

我尝试过一些事情,但我很不知所措。

for i in CIDListFile:
    if len(CID_Input) == len(i) - 1:
        print (i)
        canvas.pack()
        my_image = PhotoImage(file= ) <<<
        canvas.create_image(0, 0, anchor = NW, image=my_image)
        window.mainloop()
        CIDReplacer()

我希望输出是这样的,我会把(file = i +“。png”)和一个图像显示基于“我”是什么。但所有我都错了。我希望我在打开的tkinter图像选项卡中显示图像。

python image tkinter
2个回答
0
投票
my_image = PhotoImage(file='%s.png' % i)

您可能还需要该文件的路径。


0
投票

你可以使用一个f字符串:

my_image = PhotoImage(file=f'path/to/file/{i}.png')
© www.soinside.com 2019 - 2024. All rights reserved.