0

I have some code inside of a class. I want my load variable to be accessible by any function in my class, so I put my variable directly in the class. I get the error: NameError: name 'load' is not defined.

load = Image.open('obama.jpg')
def showImg(self):
    render = ImageTk.PhotoImage(load)

    img = Label(self, image=render)
    img.image = render
    img.place(x=0,y=0)

def hideImg(self):
    load.close()
2
  • Edit your question to show the class definition in which the code you posted lives. If possible, put an example of the invocation of the method and the error message as printed in the terminal. — That said, why on Earth the method is defined def hideImg(self):, I mean, does self serve any purpose? Commented Jul 18, 2017 at 10:14
  • @gboffi The class for this code is now long gone; I am at risk of being banned from asking more questions; what can I do to reverse this risk in light of me not having the code? Commented May 22, 2019 at 16:02

2 Answers 2

3

Access it by self.<property_name> so in this case self.load

Sign up to request clarification or add additional context in comments.

Comments

-2
global load 
load = Image.open('obama.jpg')
def showImg(self):
    global load
    render = ImageTk.PhotoImage(load)

    img = Label(self, image=render)
    img.image = render
    img.place(x=0,y=0)

def hideImg(self):
    global load
    load.close()

1 Comment

Welcome to Stack Overflow! Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.