0

I was trying to display an image in LabelFrame, but somehow it didn't work. Instead, it shows the white square. I'm currently learning GUI and Python.

I'm following this guideline to switch between Frames: Switch between two frames in tkinter

Below is my entire code:

import tkinter as tk                # python 3
import tkinter as ttk 
from tkinter import font as tkfont
from PIL import Image, ImageTk

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        
        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        width = 1280
        height = 720
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width / 2) - (width / 2)
        y = (screen_height / 2) - (height / 2)
        self.geometry(f'{width}x{height}+{int(x)}+{int(y)}')

        self.frames = {}
        for F in (Login, Register):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame


            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("Login")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class Login(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        # self.controller = controller
        self.config(bg="#FF80ED")
        lblFrame = tk.LabelFrame(self,bg="#FF80ED")
        lblFrame.place(x=490,y=160,width=340, height=450)

        # img1=Image.open("images/login.png").resize((100,100), Image.ANTIALIAS)
        # self.photoimage1=ImageTk.PhotoImage(img1)
        # lblimg1=tk.Label(image=self.photoimage1, bg="#FF80ED", borderwidth=0)
        # lblimg1.place(x=610, y=168, width=100,height=100)
        # img1 = ImageTk.PhotoImage(Image.open("images/login.png"))

        img1=Image.open("images/login.png").resize((100,100), Image.ANTIALIAS)
        photoimage1=ImageTk.PhotoImage(img1)
        lblimg1=tk.Label(lblFrame,image=photoimage1)
        lblimg1.place(x=130, y=120, width=100,height=100)
        lblimg1.pack()

class Register(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.bg1=ImageTk.PhotoImage(file="images/Nailwall.jpg")
        lblLeft=tk.Label(self, image=self.bg1)
        lblLeft.place(x=50,y=100,width=470,height=550)


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

I was stuck for 3 hours. I appreciate the help. Thank you so much

Cristian
  • 145
  • 5
  • See solution in https://stackoverflow.com/questions/71245789/how-to-define-an-image-object/71375732#71375732. This might help understand why you code does not work. – Robin Mar 07 '22 at 22:57

0 Answers0