-1

I want to pack an image for a button press(command). But, when I press the button the label shows up but it's empty

from tkinter import *

def y():

    x = PhotoImage(file= "M7.png")
    image = Label(root, image=x).pack()

root=Tk()
Button(root,text="IDK" ,command=y).pack()
root.mainloop()

I want to use as a card viewer in a card game. It's a school project, that's why I use tkinter and python. The previous queastions I not understood, that's why I asked again.

VLKI
  • 23
  • 2
  • Make a reference `image.pic = x` in the function – Saad Apr 26 '19 at 20:57
  • I made it but than it shows a Traceback: AttributeError: 'NoneType' object has no attribute 'pic' And if I press again the button it shows again an empty label, but I want to show the picture again (2 picture below each other) – VLKI Apr 26 '19 at 21:06

1 Answers1

0

This line image = Label(root, image=x).pack() will return None value because pack returns None you can compare it by printing and see the difference.

image = Label(root, image=x).pack() 
print(image)
image = Label(root, image=x)
print(image)

Here is your code

from tkinter import *

def y():

    x = PhotoImage(file= "M7.png")
    image = Label(root, image=x)
    image.pic = x
    image.pack()

root=Tk()
Button(root,text="IDK" ,command=y).pack()
root.mainloop()
Saad
  • 3,340
  • 2
  • 10
  • 32