DEF içerisinde PhotoImage hatası

Merhaba resimdeki hatanın sebebi nedir sadece def içerisindeyken böyle bi sorun yaşıyorum.

image

def mc():
    global root

                    
    dere = PhotoImage(file="renk.png").subsample(3, 3)
    root=Tk()
    root.geometry('600x500+50+50')




                
    global l
    global fota
    global chat
    global scroll
    global asd
    root.config(bg="black")

    asd = ttk.Notebook(root)
    asd.pack()
    chat = Frame(asd,bg="blue",height=500,width=600)
    komut = Frame(asd,bg="red",height=500,width=600)

    sohbet = Frame(chat,bg="green",width=600,height=430)

    
    lere = tk.Label(chat, image=dere,bg="#1C1D22")
    lere.pack()

Merhaba,

Kodlarınızın tamamı bu mu?

dere değişkenini, root değişkenini tanımladıktan sonra tanımlayınca aynı sorunu almaya devam ediyor musunuz?

Kodun tamamı bu hata devam ediyor

from tkinter import *
from tkinter import ttk
import tkinter as tk

def mc():

                    

    root=Tk()
    root.geometry('600x500+50+50')
    dere = PhotoImage(file="renk.png").subsample(3, 3)





    root.config(bg="black")

    asd = ttk.Notebook(root)
    asd.pack()
    chat = Frame(asd,bg="blue",height=500,width=600)
    komut = Frame(asd,bg="red",height=500,width=600)

    sohbet = Frame(chat,bg="green",width=600,height=430)

    
    lere = tk.Label(root, image=dere,bg="#1C1D22")
    lere.pack()




    scroll = Scrollbar(chat)
    scroll.pack(side=RIGHT, fill=Y)
    sohbet.pack()

    input_user = StringVar()
    input_field = Entry(chat, textvariable = input_user,font=("Comic Sans MS",14),width=34,background="yellow",border=1,foreground="#bbbdc0",exportselection=0)



    input_field.pack()
    input_field.place(x=6,y=440)

    

    global liste    
    liste = Text(sohbet,yscrollcommand=scroll.set,foreground="#ff0025",exportselection=0,wrap=WORD,background="#2e2e2e",border=1,font=("Comic Sans MS",10))
    scroll.config(command=liste.yview)


    liste.pack()
    chat.pack()
    
    asd.add(chat, text="Sohbet")
    asd.add(komut, text="Komut")


    @client.event
    async def on_message(msg):

        if 'kapat' in msg.content:
            await client.close()




   
    root.mainloop()





	
def on_click(event):		
    fotos = PhotoImage(file="wait.png").subsample(3, 3)
    l["image"] = fotos
    print("image clicked")
    mc()


giriş = Tk()
global l

giriş.geometry("600x200+700+300")
giriş.config(bg="#1C1D22")
giriş.title("Yönetim Paneli [Giriş]")
foto = PhotoImage(file="play.png").subsample(3, 3)
foto2 = PhotoImage(file="settigins.png").subsample(3, 3)
foto3 = PhotoImage(file="log.png").subsample(3, 3)


l = tk.Label(giriş, image=foto,bg="#1C1D22")
l.pack()
l.place(x=10,y=10)

l2 = tk.Label(giriş, image=foto2,bg="#1C1D22")
l2.pack()
l2.place(x=220,y=10)

l3 = tk.Label(giriş, image=foto3,bg="#1C1D22")
l3.pack()
l3.place(x=420,y=10)
l.bind("<Button-1>",on_click)
giriş.mainloop()

mc() fonksiyonu içindeki root'u tk sınıfından değil Toplevel sınıfından oluşturun ve fonksiyonun son satırında yer alan root.mainloop()'u kaldırın.

Bu sefer çalıştı ama resim ortada yok

from tkinter import *
from tkinter import ttk
import tkinter as tk


def mc():

                    
    dere = PhotoImage(file="renk.png")
    root=Toplevel()
    root.geometry('600x500+50+50')
    





    root.config(bg="black")

    asd = ttk.Notebook(root)
    asd.pack()
    chat = Frame(asd,bg="blue",height=500,width=600)
    komut = Frame(asd,bg="red",height=500,width=600)

    sohbet = Frame(chat,bg="green",width=600,height=430)

    





    scroll = Scrollbar(chat)
    scroll.pack(side=RIGHT, fill=Y)
    sohbet.pack()

    input_user = StringVar()
    input_field = Entry(chat, textvariable = input_user,font=("Comic Sans MS",14),width=34,background="yellow",border=1,foreground="#bbbdc0",exportselection=0)



    input_field.pack()
    input_field.place(x=6,y=440)

    

    global liste    
    liste = Text(sohbet,yscrollcommand=scroll.set,foreground="#ff0025",exportselection=0,wrap=WORD,background="#2e2e2e",border=1,font=("Comic Sans MS",10))
    scroll.config(command=liste.yview)


    liste.pack()
    chat.pack()
    
    asd.add(chat, text="Sohbet")
    asd.add(komut, text="Komut")

    lere = tk.Label(root, image=dere,bg="#1C1D22").pack()









	
def on_click(event):		
    fotos = PhotoImage(file="wait.png").subsample(3, 3)
    l["image"] = fotos
    print("image clicked")
    mc()


giriş = Tk()
global l

giriş.geometry("600x200+700+300")
giriş.config(bg="#1C1D22")
giriş.title("Yönetim Paneli [Giriş]")
foto = PhotoImage(file="play.png").subsample(3, 3)
foto2 = PhotoImage(file="settigins.png").subsample(3, 3)
foto3 = PhotoImage(file="log.png").subsample(3, 3)


l = tk.Label(giriş, image=foto,bg="#1C1D22")
l.pack()
l.place(x=10,y=10)

l2 = tk.Label(giriş, image=foto2,bg="#1C1D22")
l2.pack()
l2.place(x=220,y=10)

l3 = tk.Label(giriş, image=foto3,bg="#1C1D22")
l3.pack()
l3.place(x=420,y=10)
l.bind("<Button-1>",on_click)
mc()
giriş.mainloop()

root.img = dere ifadesini root ve dere'nin altına yazarsanız, resim görünür diye düşünüyorum.

global dere yaptıgımda resim sıkıntısız gözüküyor anlam veremedim çok saçma geldi

Saçma değil, resmin ya global alanda tanımlanması lazım ya da bir widgete bağlanması gerekiyor.

http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm

1 Beğeni

bilgilendirdiginiz için teşekkürler :slight_smile: