Tkinter未显示图像,也没有错误。

huangapple go评论84阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月30日 08:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定