0

1

from PIL import ImageTk
import PIL.Image

from tkinter import*
from tkinter import messagebox
class login_interface(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.master=master
        self.security()          

    def security(self):
        self.master.title("LOGIN")
        self.pack(fill=BOTH, expand=1)
        lbl = Label(self, text="PASSWORD").grid(column=0, row=0)
        self.pwrd = Entry(self,show="*")
        self.pwrd.focus_set()
        self.pwrd.grid(column=1, row=0)
        login = Button(self, text="login", width=20,command=self.login)
        login.grid(column=1, row=2)


    def login(self):
        if self.pwrd.get()=="a":
            a.destroy()
            b=Tk()
            b.geometry("%dx%d" % (width, height))
            app_b=question(b) 
            app_b.mainloop()
        else:
            messagebox.showinfo("ERROR ","retry wrong password")

class question(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.master=master

        self.master.title("Question")
        self.pack(fill=BOTH, expand=1)
        test = Label(self, image=ImageTk.PhotoImage((PIL.Image.open(r"tree.jpg").resize((50,50)))))
        test.pack()
        lbl = Label(self, text="Who made the pythagoras theorem?").place(relx=0.5, rely=0.3, anchor='center')
        self.my_guess = Entry(self)
        self.my_guess.place(relx=0.5, rely=0.5, anchor='center')
        my_enter = Button(self, text="Enter", command=self.check).place(relx=0.5, rely=0.7, anchor='center')


    def check(self):
        if self.my_guess.get() == "Pythagoras":
            messagebox.showinfo("Correct","Go to Next Quiz.")
        else:
            messagebox.showinfo("Wrong", "Try Again.")

a=Tk()
width= a.winfo_screenwidth()               
height= a.winfo_screenheight()               
a.geometry("%dx%d" % (width, height))
app=login_interface(a)
app.mainloop()

I have been trying to find the problem in the code from a long time, the tree.jpg file doesn't appear in the screen at all, even after checking that the photo is in the same folder as the program.I would be really grateful if someone could help me find the problem in the code.

i have tried changing the photos and cross checking photo locations, but still the photo doesn't show up

vimuth
  • 5,064
  • 33
  • 79
  • 116
ADS
  • 1
  • 1

1 Answers1

0

Here are the problems

  • First, you are trying to display the image using a Label widget, but you are not actually adding the Label widget to the window. You can fix this by using the pack() method to add the Label widget to the window.

  • Second, you are trying to set the image attribute of the Label widget to an ImageTk.PhotoImage object, but you are not actually creating an ImageTk.PhotoImage object. Instead, you are trying to pass an Image object to the PhotoImage constructor. To fix this, you can create an ImageTk.PhotoImage object by passing the Image object to the PhotoImage constructor as follows:

      image = PIL.Image.open(r"tree.jpg").resize((50,50))
    
      image = ImageTk.PhotoImage(image)
    
      test = Label(self, image=image)
    
      test.pack()