Home > Software engineering >  UnboundLocalError: local variable 'pop' referenced before assignment
UnboundLocalError: local variable 'pop' referenced before assignment

Time:06-06

Full text error console

Exception in thread Thread-3 (make_jenya):
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in 
_bootstrap_inner
self.run()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Admin\Desktop\biron\main.py", line 26, in make_jenya
pop = Toplevel(pop)
UnboundLocalError: local variable 'pop' referenced before assignment

I want to have an image in the background, but it is not displayed and gives an error. I know that this topic has been discussed many times, but I still can not understand what the problem is.

Tell me, I'll be glad

==============================================================

My code:

from tkinter import *
from tkinter import messagebox
from PIL import ImageTk, Image
from threading import Thread
from playsound import playsound
import webbrowser

root = Tk()

url = 'https://www.youtube.com/'

def OpenUrl(url):
    webbrowser.open_new(url)

def video():
    Thread(target = lambda: OpenUrl(url), daemon=True).start()

def music():
    playsound(r"C:\Users\Admin\Desktop\biron\horror_music.mp3")

def sound_clicked_btn():
    playsound(r"C:\Users\Admin\Desktop\biron\btn_click.mp3")

def make_jenya():

    pop = Toplevel(pop)
    pop.title("SUPERJENYA.EXE KILLER")
    c=Canvas(pop,bg="gray16", height=200,width=200)
    pop.geometry("500x350")
    pop.resizable(height = False, width=False)

    filename=PhotoImage(file="C://Users//Admin//Desktop//biron//jenya.png")
    background_label=Label(pop,image=filename)
    background_label.place(x=0,y=0, relwidth=1, relheight=1)

    c.pack()




def btn_click():
   if messagebox.askyesno("ЕВГЕНИЙ.EXE", "Вы уверены?") == True:
    Thread(target = sound_clicked_btn, daemon=True).start()
    Thread(target = make_jenya, daemon=True).start()

root['bg']='#fafafa'
root.geometry('300x250')
root.title('Евгений супер мозг')
root.resizable(height = False, width=False)
root.iconphoto(True, PhotoImage(file=('jenya.png')))
Thread(target = music, daemon=True).start()

canvas = Canvas(root, height=300, width=250)

canvas.pack()

frame = Frame(root, bg='red')
frame.place(relx=0.15, rely=0.15, relwidth=0.7, relheight=0.7)
title = Label(frame,text='Увидеть СуперЖеню?', bg='gray', font=40)
title.pack()
btn = Button(frame, text='Да, хочу его увидеть', bg='yellow', height=5, width=8, command=btn_click)
btn.pack()

btn_video = Button(frame, text='Посмотреть видео \n"Суперженя уже не бог"?', bg='blue', height=2, width=25, command=video)
btn_video.pack()


root.mainloop()

CodePudding user response:

The answer is simple, you must define a value to the variable pop. Here is a simpler example of what you are trying to do:

x = int(x)

This will error as well because x has no value and you cant pass None type to int function.

To fix your code just define a value for pop

CodePudding user response:

The error message:

pop = Toplevel(pop)
UnboundLocalError: local variable 'pop' referenced before assignment

is saying that this pop doesn't have a value (has not been assigned to yet):

pop = Toplevel(pop)
               ^^^
  • Related