-1

Not sure what is happening but I cannot get Test_Image.jpg to appear, the text looks fine.

I'm using PyCharm as my IDE (just giving random info now, stack overflow wants me to) I have done some searches on this topic, but didn't get anything helpful...

# - Script Handler

# ==== Script Info ====
#   6 / 10 / 2020
# Gun_Generator.Script_Handler.py

# ==== Description ===
# Takes information from other scripts and displays them on a GUI

# ==== Code ====
# Imports Packages
import PIL.Image
from PIL import ImageTk
from tkinter import *
import os

class GUI_Controler:
    def __init__(self, width, height):
        self.instance_Window = None
        self.temp_Canvas = None

        self.CreateWindow(width, height)

    def CreateWindow(self):
        self.instance_Window = Tk(className = "NAME")

    def CreateWindow(self, width, height):
        self.instance_Window = Tk(className = "NAME")
        self.instance_Window.minsize(width, height)

    def Load_Image(self, image_To_Load):
        loadedImage = PIL.Image.open(str(image_To_Load))
        self.convertedImage = PhotoImage(file = image_To_Load)

        self.temp_Canvas = Canvas(self.instance_Window, height=self.instance_Window.winfo_height(), width=self.instance_Window.winfo_width())
        self.temp_Canvas.pack()
        self.temp_Canvas.create_image(0, 0, image=self.convertedImage, anchor=CENTER)

        loadedImage.save("C:/Users/Luke's PC/PycharmProjects/Gun_Generator/Resources/Images/Tests/Test_Image_SAVED.png")

    def Load_Text(self, text):
        text_label = Label(self.instance_Window, text = text)
        text_label.pack()


resource_dir = "C:/Users/Luke's PC/PycharmProjects/Gun_Generator/Resources"

main = GUI_Controler(750, 500)

main.Load_Image(resource_dir + "/Images/Tests/Test_Image.jpg")
main.Load_Text("test")

mainloop()
  • Does this answer your question? [why-does-tkinter-image-not-show-up-if-created-in-a-function](https://stackoverflow.com/questions/16424091) – stovfl Jun 11 '20 at 09:37
  • Try `ImageTk.PhotoImage(...)` instead of `PhotoImage(...)` – acw1668 Jun 11 '20 at 12:00
  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – jasonharper Jun 11 '20 at 12:23

1 Answers1

0

I have checked your code and there is nothing wrong from loading the image from within your class. However, I noticed the create_image is causing the problem here. I'm not entirely sure why, but I can answer your question showing you how to get your image shown on your widget by using label image.

# - Script Handler

# ==== Script Info ====
#   6 / 10 / 2020
# Gun_Generator.Script_Handler.py

# ==== Description ===
# Takes information from other scripts and displays them on a GUI

# ==== Code ====
# Imports Packages
import PIL.Image
from PIL import ImageTk
from tkinter import *
import os

class GUI_Controler:
    def __init__(self, width, height):
        self.instance_Window = None
        self.temp_Canvas = None

        self.CreateWindow(width, height)

    def CreateWindow(self):
        self.instance_Window = Tk(className = "NAME")

    def CreateWindow(self, width, height):
        self.instance_Window = Tk(className = "NAME")
        self.instance_Window.minsize(width, height)

    def Load_Image(self, image_To_Load):
        loadedImage = PIL.Image.open(str(image_To_Load))
        self.convertedImage = PhotoImage(file = image_To_Load)

        self.temp_Canvas = Canvas(self.instance_Window, height=self.instance_Window.winfo_height(), width=self.instance_Window.winfo_width())
        self.temp_Canvas.pack()
        label=Label(self.temp_Canvas, image=self.convertedImage, width=400, height=400)
        label.pack(side=TOP)

        loadedImage.save("C:/Users/Luke's PC/PycharmProjects/Gun_Generator/Resources/Images/Tests/Test_Image_SAVED.png")

    def Load_Text(self, text):
        text_label = Label(self.instance_Window, text = text)
        text_label.pack()


resource_dir = "C:/Users/Luke's PC/PycharmProjects/Gun_Generator/Resources"

main = GUI_Controler(750, 500)

main.Load_Image(resource_dir + "/Images/Tests/Test_Image.jpg")
main.Load_Text("test")

mainloop()
Nicholas TJ
  • 1,629
  • 18
  • 30