英文:
Tkinter not showing image and no errors
问题
以下是翻译好的代码部分:
def show_restaurant_info(event):
global photo
selected_restaurant = restaurant_listbox.get(tk.ACTIVE)
if selected_restaurant:
restaurant = next((r for r in restaurants if r["name"] == selected_restaurant), None)
if restaurant:
info_label.delete('1.0', tk.END)
restaurant_name = restaurant["name"]
restaurant_postal_code = restaurant["postal_code"]
restaurant_cuisine = restaurant["cuisine"]
restaurant_price = restaurant["price"]
restaurant_reviews = restaurant["reviews"]
# Display restaurant information
info_label.insert(tk.END, f"""Name: {restaurant_name}
Postal Code: {restaurant_postal_code}
Cuisine: {restaurant_cuisine}
Price: {restaurant_price}
Reviews: {restaurant_reviews}
""")
# Load and display restaurant image (GIF)
image_path = restaurant["image"]
try:
#photo = tk.PhotoImage(file="image_path")
photo = Image.open(image_path)
photo = photo.resize((50, 50))
# Create a PhotoImage object from the image
img_tk = ImageTk.PhotoImage(photo)
# Update the label with the new image
image_label.configure(image=img_tk)
#image_label.configure(image=photo)
image_label.image = photo
except (IOError, tk.TclError):
# If image file is not found, display a default image
default_image = ImageTk.PhotoImage(Image.open("default_image.gif"))
image_label.configure(image=default_image)
image_label.image = default_image
英文:
The code below does not show an image when run.
def show_restaurant_info(event):
global photo
selected_restaurant = restaurant_listbox.get(tk.ACTIVE)
if selected_restaurant:
restaurant = next((r for r in restaurants if r["name"] == selected_restaurant), None)
if restaurant:
info_label.delete('1.0', tk.END)
restaurant_name = restaurant["name"]
restaurant_postal_code = restaurant["postal_code"]
restaurant_cuisine = restaurant["cuisine"]
restaurant_price = restaurant["price"]
restaurant_reviews = restaurant["reviews"]
# Display restaurant information
info_label.insert(tk.END, f"""Name: {restaurant_name}
Postal Code: {restaurant_postal_code}
Cuisine: {restaurant_cuisine}
Price: {restaurant_price}
Reviews: {restaurant_reviews}
""")
# Load and display restaurant image (GIF)
image_path = restaurant["image"]
try:
#photo = tk.PhotoImage(file="image_path")
photo = Image.open(image_path)
photo = photo.resize((50, 50))
# Create a PhotoImage object from the image
img_tk = ImageTk.PhotoImage(photo)
# Update the label with the new image
image_label.configure(image=img_tk)
#image_label.configure(image=photo)
image_label.image = photo
except (IOError, tk.TclError):
# If image file is not found, display a default image
default_image = ImageTk.PhotoImage(Image.open("default_image.gif"))
image_label.configure(image=default_image.gif)
image_label.image = default_image.gif
答案1
得分: 1
你保存了错误的图像引用 photo
。你需要保存图像引用 img_tk
:
def show_restaurant_info(event):
...
try:
...
image_label.image = img_tk # 保存图像引用
except (IOError, tk.TclError):
# 如果找不到图像文件,显示默认图像
default_image = ImageTk.PhotoImage(Image.open("default_image.gif"))
image_label.configure(image=default_image) # 修复了拼写错误 default_image.gif
image_label.image = default_image
英文:
You have saved the wrong image reference photo
. You need to save the image reference img_tk
instead:
def show_restaurant_info(event):
...
try:
...
image_label.image = img_tk # save reference of image
except (IOError, tk.TclError):
# If image file is not found, display a default image
default_image = ImageTk.PhotoImage(Image.open("default_image.gif"))
image_label.configure(image=default_image) # fixed typo default_image.gif
image_label.image = default_image
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论