-1

I am new to Python and trying to play with Tkinter and Image placement in frames.

I am wanting to create a function that will allow me to place an image into any frame. Below is the function I have created.

The code in the function works by itself (outside of a function) but does not produce the same result in the function. Outside the function it gives me the image in the frame, however in the function (unless I show the image first - commented out code) it produces a black colored frame.

I have seen that there are numerous threads similar to this problem, but none make sense to me.


# Create a function to place an image into a set frame

def PlaceImageInFrame(ImageURL, Frame, NewWidth, NewHeight, backgroundColor):
    # Import the Login Logo
    Login_Image = Image.open(ImageURL)

    # Shrink Image
    Login_Image.thumbnail((NewWidth,NewHeight))

    # Show Image
    #Login_Image.show()

    # Bring Image into Python Tkinter
    Login_Logo = ImageTk.PhotoImage(Login_Image)  # PIL solution
    
    # Create New Label in a Frame
    Logo_Image = Label(Frame, image = Login_Logo, bg=backgroundColor)

    # Place Label in the Frame
    Logo_Image.place(x=0, y=0)

PlaceImageInFrame("Logo.jpg",Login_Logo_Frame, 400, 400, "black")

Edit 1: For example, other stack overflow posts i have seen include: Why does Tkinter image not show up if created in a function?

[Solution] Edit 2: Found an interesting post on Github that shows the fix is as simple as reassigning the image to the label - a little strange.

https://github.com/ythy/blog/issues/302

"
photo = PhotoImage(...)
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
"

1 Answers1

0

you could not call address image as direct

  • first by PhotoImage class fetch this image ,then put in parameter
image=PhotoImage(file="Logo.jpg") 
PlaceImageInFrame(iamge,Login_Logo_Frame, 400, 400, "black")
asd a
  • 1
  • 1