Как просмотреть изображение в python tkinter с помощью PhotoImage

#python #tkinter

#python #tkinter

Вопрос:

Пожалуйста, я создаю представление приложения и в python и пытаюсь показать некоторые изображения, но получаю ошибку. ниже приведен код

 from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Developing Software")
root.iconbitmap("icon.ico")

image = Image.open("tree_50b.JPG").convert("RGB")
my_img = ImageTk.PhotoImage(image)
my_label = Label(my_img)
my_label.pack()

button_quit = Button(root, text='Exit Program', fg='red', command=root.quit)
button_quit.pack()

root.mainloop()
  

Это сообщение об ошибке

 Traceback (most recent call last):

  File "<ipython-input-1-c9a970a4c796>", line 16, in <module>
    my_label = Label(my_img)

  File "C:ancondalibtkinter__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)

  File "C:ancondalibtkinter__init__.py", line 2292, in __init__
    BaseWidget._setup(self, master, cnf)

  File "C:ancondalibtkinter__init__.py", line 2269, in _setup
    if master._last_child_ids is None:

AttributeError: 'PhotoImage' object has no attribute '_last_child_ids'
  

Мне нужна помощь, пожалуйста

Ответ №1:

Первым аргументом Label(...) должен быть виджет, а не изображение, а изображение должно быть аргументом ключевого слова.

 Label(root, image=my_img)