I need to show an image into a label or a checkButton, for this aim, I am using Pillow and Tkinter. But I am wondering what is going on with my codes, I mean, I have done two tests, the first one works perfectly but the second one doesn’t show the required image, all what is shown is a blank background. Really, I have not found the problem yet. Would you help me, please?
Thank you in advance. First code:
from tkinter import ttk
from tkinter.ttk import *
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
myImg = ImageTk.PhotoImage(Image.open("firstFunction.png").resize([100, 100]))
ttk.Label(root, image=myImg).pack()
root.mainloop()
Second code:
from tkinter import ttk
from tkinter.ttk import *
from tkinter import *
from PIL import ImageTk, Image
class Gui:
    # Constructor function
    def __init__(self, title, width, height, foreground, background):
        # Attributes of the instance
        self.root = Tk()  # Root
        self.title = title  # Title
        self.height = height  # Height
        self.width = width  # Width
        self.foreground = foreground  # Foreground
        self.background = background  # Background color
    # Method of the class
    # Function for creating the appareance of Gui
    def appareance(self):
        # Setting tittle
        self.root.title(self.title)
        # Size of screen
        self.root.geometry(str(self.width) + "x" + str(self.height))
        # Changing background color
        self.root.configure(bg=self.background)
    # Function to create a menu
    def menu(self):
        # Creating main menu
        mainMenu = Menu(self.root)
        # Setting main menu
        self.root.config(menu=mainMenu)
        # Creating submenu of main menu
        plot = Menu(mainMenu, tearoff=FALSE)
        mainMenu.add_cascade(label="Plot", menu=plot)
        plot.add_command(label="test", command=self.secondMenu)
    # Function to create the second menu
    def secondMenu(self):
        # Creating a second window
        secondWindow = Toplevel(self.root, background="white")
        myImg = ImageTk.PhotoImage(Image.open("firstFunction.png").resize([100, 100]))
        ttk.Label(secondWindow, image=myImg).grid()
    # Function for calling each function from the class
    def gui(self):
        self.appareance()
        self.menu()
        # Creating infinite loop to show in screen
        self.root.mainloop()
# Main features of a GUI
title = "test"
width = 400
height = 450
foreground = "black"
background = "white"
# Creating an object from classGUI
a = Gui(title, width, height, foreground, background)
a.gui()
 
    