Issue
This is my entire code:
import tkinter
from tkinter import ttk
import screen # my own local module
window = tkinter.Tk()
frame = ttk.Frame(window, width=1920, height=1080)
image = screen.get_photoimage("img/background.png", contain_size=(1920, 1080))
label = tkinter.Label(frame, image=image)
label.place(x=0, y=0, relwidth=1, relheight=1)
window.mainloop()
screen.get_photoimage()
just creates a PhotoImage
contained to the given dimensions.
When I run it, it just displays an all-grey window instead of my image. However, when I write:
import tkinter
import screen # my own local module
window = tkinter.Tk()
image = screen.get_photoimage("img/background.png", contain_size=(1920, 1080))
label = tkinter.Label(window, image=image)
label.place(x=0, y=0, relwidth=1, relheight=1)
window.mainloop()
It does display my image.
So I can see my image when I place the Label
it's on onto a Tk
object but not when I place it onto a Frame
object. I'm really confused as to why this is.
I don't mind placing it onto some parent other than a Frame
. The reason why I can't place it directly onto a Tk
is because it's for a bigger project where I would like to be able to destroy the Frame
that contains the image (and will also contain some other things) while keeping the window open; if I ran destroy()
on the Tk
object that would close the window. So if it's something to do with the Frame
class itself, I'm happy to swap it out for e.g. a Canvas
or something if someone knows a class that would work, but putting it directly onto the Tk
object is not feasible for me. Thanks!
Solution
I forgot to call pack
/grid
/place
on the frame—now works as expected
Answered By - revsuine
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.