我有一个for循环,生成4个按钮,每个按钮都指向一个函数。此函数需要从清除页面上的所有按钮开始。你能帮个忙吗?
x_position = 160
for i in range (5,9):
u = urllib.request.urlopen(actorimages[i])
raw_data = u.read()
u.close()
im = Image.open(BytesIO(raw_data))
im = im.resize((120,180),Image.ANTIALIAS)
photo = ImageTk.PhotoImage(im)
photo1button = tk.Button(image=photo,width=120,height=180,compound="c",
borderwidth = 0,
highlightthickness = 0,
command = lambda i=i, b=photo1button: actorInfo(i,canvas,b),
relief = "flat")
photo1button.image = photo
photo1button.place(x=x_position,y=351)
canvas.create_text(
x_position, 552,
text = actorName[i],
fill = "#ffffff",
anchor=tk.SW,
tag = "actorheading",
font = ("Roboto", int(9.0)))
x_position += 158
def actorInfo(i,canvas,b):
b.place_forget()
canvas.delete("actorheading")
发布于 2022-02-20 14:57:39
您可以使用一个列表来存储按钮的所有引用,然后可以通过这个列表删除actorInfo()
中的按钮。
buttons = [] # list to store the created buttons
x_position = 160
for i in range (5,9):
u = urllib.request.urlopen(actorimages[i])
raw_data = u.read()
u.close()
im = Image.open(BytesIO(raw_data))
im = im.resize((120,180),Image.ANTIALIAS)
photo = ImageTk.PhotoImage(im)
photo1button = tk.Button(image=photo,width=120,height=180,compound="c",
borderwidth = 0,
highlightthickness = 0,
command=lambda: actorInfo(canvas),
relief = "flat")
photo1button.image = photo
photo1button.place(x=x_position,y=351)
buttons.append(photo1button) # add button to list
canvas.create_text(
x_position, 552,
text = actorName[i],
fill = "#ffffff",
anchor=tk.SW,
tag = "actorheading",
font = ("Roboto", int(9.0)))
x_position += 158
def actorInfo(canvas):
# remove all buttons (note that they are not destroyed) stored in the list
for b in buttons:
b.place_forget()
canvas.delete("actorheading")
请注意,我已经删除了i
和b
参数在actorInfo()
中,因为这是不必要的。实际上,您也可以删除i
参数。
https://stackoverflow.com/questions/71193420
复制相似问题