【发布时间】:2022-12-03 14:22:50
【问题描述】:
你好,所以我一直在努力解决这个问题,但我试过字典和 exec 找不到任何东西。如何使用字符串值作为变量名?当我在字符串中定义变量名称并尝试使用显示错误的图像创建按钮时出现问题 - _tkinter.TclError:图像“score6”不存在,但如果我手动输入图像变量名称错误不显示。
img = 'score' + str(correct) #here I make the variable name #the scores can be from 0-9
self.rez = Button(window, relief="sunken", image=img, bd=0, bg='#cecece',activebackground='#cecece')
self.rez.place(x=520, y=330)
#这是定义图像的地方(这是在类之外)
score0 = ImageTk.PhotoImage(Image.open("scores/09.png"))
score1 = ImageTk.PhotoImage(Image.open("scores/19.png"))
score2 = ImageTk.PhotoImage(Image.open("scores/29.png"))
score3 = ImageTk.PhotoImage(Image.open("scores/39.png"))
score4 = ImageTk.PhotoImage(Image.open("scores/49.png"))
score5 = ImageTk.PhotoImage(Image.open("scores/59.png"))
so how can I use string value as a variable name?
【问题讨论】:
-
您有多个
Tk实例吗? -
@Thingamabobs 我认为不