Closed GloriousGlider8 closed 2 months ago
Like tkinter, TKMT doesn't have a direct image function, but an image can be added using a label.
import TKinterModernThemes as TKMT
from PIL import ImageTk, Image
class App(TKMT.ThemedTKinterFrame):
def __init__(self, theme, mode, usecommandlineargs=True, usethemeconfigfile=True):
super().__init__("Image", theme, mode, usecommandlineargs=usecommandlineargs,
useconfigfile=usethemeconfigfile)
img = ImageTk.PhotoImage(Image.open('sample_image.png').resize((200, 200)))
self.frame = self.addLabelFrame("Frame")
self.frame.Label("", widgetkwargs={"image": img})
self.run()
if __name__ == "__main__":
App("park", "dark")
Please let me know if you need anything else!
Thanks! I was trying to use the Label class from TKinter it's self!
Still does not work for some reason! It seems to be an issue just with my code!
try:
download_png(gm.data["assets"]["cover-tiny"]["uri"], f"{os.getenv("TEMP")}/{gm.data["abbreviation"]}_cvr.png")
img = ImageTk.PhotoImage(Image.open(f"{os.getenv("TEMP")}/{gm.data["abbreviation"]}_cvr.png"))
except:
img = ImageTk.PhotoImage(Image.open("resources/cover.png"))
temp = self.games.addFrame("GMFrame" + gm.name, pady=0)
temp.Label("", widgetkwargs={"image": img})
temp.Text(gm.name, ("Segoe UI bold", 18))
Maybe it is resizing...
Nope, resizing to (200, 200) does not work
I have tried putting the image in it's own frame and it still does not work. :(
For the record, I am on Windows 11 Home Edition, with the latest TKinter and TKMT versions, on python 3.12.1
I'm very sorry if I'm flooding your inbox! When I add text, the text shows, maybe it is to do with prioritising the text?
Again, I apologise for the inbox spam! I've managed it! So that everyone reading this knows, the trick is to make a global reference to the image and use .config(image=img) and .image = img (it worked for me)
It is mainly due to the python garbage collector
Yes - this is a known issue with tkinter:
https://dafarry.github.io/tkinterbook/photoimage.htm
Note: When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget.
To avoid this, the program must keep an extra reference to the image object. A simple way to do this is to assign the image to a widget attribute, like this:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
First off, no WidgetFrame.Image() function. Even when using TKinter's labels, with self.master as the master, images do not work, even when I add them to the widget list.