标题可与错误行一起使用,但在删除糟糕的线时不会固定运行

问题描述 投票:0回答:1

)。但是,该程序即使在遇到错误后也可以按预期工作。如果删除这一行,图像将不会显示我无法弄清楚为什么 import tkinter as tk from PIL import Image, ImageTk import string # string.ascii_lowercase = "abcdefghijklmnopqrstuvwxyz" def on_enter(event): global image_label btn=str(event.widget) match btn: case ".1": img = Image.open("District_1b.png") case ".2": img = Image.open("District_2b.png") case ".3": img = Image.open("District_3b.png") case ".4": img = Image.open("District_4b.png") case ".5": img = Image.open("District_5b.png") case ".6": img = Image.open("District_6b.png") case ".7": img = Image.open("Districtsb.png") img = ImageTk.PhotoImage(img) image_label.configure(image=img) photo = ImageTk.PhotoImage(img) def on_leave(event): global image_label #image_label.destroy() root = tk.Tk() button1 = tk.Button(root, text="District I", name="1") button1.grid(row=0,column=0) button2 = tk.Button(root, text="District II", name="2") button2.grid(row=0,column=1) button3 = tk.Button(root, text="District III", name="3") button3.grid(row=0,column=2) button4 = tk.Button(root, text="District IV", name="4") button4.grid(row=0,column=3) button5 = tk.Button(root, text="District V", name="5") button5.grid(row=0,column=4) button6 = tk.Button(root, text="District VI", name="6") button6.grid(row=0,column=5) button7 = tk.Button(root, text="All District's", name="7") button7.grid(row=0,column=6) img = Image.open("Screenshot 2024-12-13 144137.png") img = ImageTk.PhotoImage(img) image_label = tk.Label(root,width=1400, image=img) image_label.grid(row=1,column=0, columnspan=8) button1.bind("<Enter>", on_enter) button1.bind("<Leave>", on_leave) button2.bind("<Enter>", on_enter) button2.bind("<Leave>", on_leave) button3.bind("<Enter>", on_enter) button3.bind("<Leave>", on_leave) button4.bind("<Enter>", on_enter) button4.bind("<Leave>", on_leave) button5.bind("<Enter>", on_enter) button5.bind("<Leave>", on_leave) button6.bind("<Enter>", on_enter) button6.bind("<Leave>", on_leave) button7.bind("<Enter>", on_enter) button7.bind("<Leave>", on_leave) root.mainloop() 我知道photoImage“对象没有属性”,但是,如果我将其评论或简单地删除。图像只是显示一个空白区域。如果剩下错误,我可以将每个按钮悬停,即使已命中错误,图像也会按预期更新。

sissue解释
您遇到的问题是由Python的垃圾收集机制引起的。
您正在创建一个photoimage对象(img = imagetk.photoimage(img))on_enter函数。
但是,当功能结束时,如果您不关注IMG的引用,则Python将自动删除它。
这就是为什么当您删除看似冗余的线照片= Imagetk.photoimage(IMG)时,您的图像也会消失的原因,即使它从未使用过。
解决方案
要解决此问题,请将对IMG的引用存储为全局变量:
fixed代码:

import tkinter as tk from PIL import Image, ImageTk def on_enter(event): global image_label, current_image btn = str(event.widget) match btn: case ".1": img = Image.open("District_1b.png") case ".2": img = Image.open("District_2b.png") case ".3": img = Image.open("District_3b.png") case ".4": img = Image.open("District_4b.png") case ".5": img = Image.open("District_5b.png") case ".6": img = Image.open("District_6b.png") case ".7": img = Image.open("Districtsb.png") current_image = ImageTk.PhotoImage(img) # Store in global variable image_label.configure(image=current_image) def on_leave(event): global image_label # Optionally, reset to a default image when leaving # image_label.configure(image=default_image) root = tk.Tk() button1 = tk.Button(root, text="District I", name="1") button1.grid(row=0,column=0) button2 = tk.Button(root, text="District II", name="2") button2.grid(row=0,column=1) button3 = tk.Button(root, text="District III", name="3") button3.grid(row=0,column=2) button4 = tk.Button(root, text="District IV", name="4") button4.grid(row=0,column=3) button5 = tk.Button(root, text="District V", name="5") button5.grid(row=0,column=4) button6 = tk.Button(root, text="District VI", name="6") button6.grid(row=0,column=5) button7 = tk.Button(root, text="All District's", name="7") button7.grid(row=0,column=6) # Load initial image default_image = Image.open("Screenshot 2024-12-13 144137.png") default_image = ImageTk.PhotoImage(default_image) image_label = tk.Label(root, width=1400, image=default_image) image_label.grid(row=1, column=0, columnspan=8) # Keep a global reference for images current_image = default_image # Bind events for btn in (button1, button2, button3, button4, button5, button6, button7): btn.bind("<Enter>", on_enter) btn.bind("<Leave>", on_leave) root.mainloop()
python tkinter
1个回答
0
投票
关键修复: 添加了一个全局变量current_image来存储摄影图的引用。 删除照片= Imagetk.photoimage(IMG)(由于不必要)。 确保image_label.configure(image = current_image)使用存储的图像参考,以防止垃圾收集删除它。 现在,即使删除行27时,您的图像也应正确显示。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.