0

I have defined some variables in method 1 and i call method 2 from it. In method 2, i have to access method 1 variable.

class Window(Frame):

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

    def myElements(self):
        self.master.title("1841144-SANDRINE P JOY")
        self.pack(fill=BOTH , expand=1)
        label1=Label(text="Starts with").place(x=10,y=0)
        generateButton = Button(self,text="Generate", command=self.autogen)
        generateButton.place(x=150,y=0)
        label2=Label(text="--Here , your Random Name appears--").place(x=70,y=30)

    def autogen(self):
        randomName=pick(names)
        label2.insert(INSERT,randomName) #This line is wrong

Thanks @AKX & @Cool Cloud for figuring my errors

Here is the corrected code

creating an instance of a method of the variable was all needed for it

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

    def myElements(self):
        self.master.title("1841144-SANDRINE P JOY")
        self.pack(fill=BOTH , expand=1)
        label1=Label(text="Starts with").place(x=10,y=0)
        generateButton = Button(self,text="Generate", command=self.autogen)
        generateButton.place(x=150,y=0)
        self.result=Text(self,width=10,height=1)
        self.result.place(x=70,y=30)
        mstart = Label(self, text="Starting with M",bg='white',fg='black',font=("italic", 10)).place(x=20,y=50)
        mtext = Text(self, width=15, height=10)
        mtext.place(x=10,y=70)

        pstart = Label(self, text="Starting with P",bg='white',fg='black',font=("italic", 10)).place(x=140,y=50)
        ptext = Text(self, width=15, height=10)
        ptext.place(x=120,y=70)

        nstart = Label(self, text="Starting with N",bg='white',fg='black',font=("italic", 10)).place(x=260,y=50)
        ntext = Text(self, width=15, height=10)
        ntext.place(x=240,y=70)     

    def autogen(self):
        randomName=pick(names)
        self.result.insert(INSERT,randomName)
4
  • The variable label2 ceases to exist as soon as myElements() returns. autogen() cannot possibly access it, because there's nothing to access. Store it as an instance attribute, instead. Commented Sep 5, 2020 at 18:03
  • @jasonharper wouldnt saying self.label2=Label(...) and then self.label2.insert(...) work? Commented Sep 5, 2020 at 18:10
  • @CoolCloud no , it doesn't work Commented Sep 5, 2020 at 18:24
  • @SandrinJoy You have to say self.label2 = Label(...) then self.label2.place(..) then say self.label2.insert(..) but i believe Label does not have insert() method Commented Sep 5, 2020 at 18:33

1 Answer 1

1

Store them as instance variables (self.label1 etc.):

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

    def myElements(self):
        self.master.title("1841144-SANDRINE P JOY")
        self.pack(fill=BOTH, expand=1)
        self.label1 = Label(text="Starts with")
        self.label1.place(x=10, y=0)
        self.generateButton = Button(self, text="Generate", command=self.autogen)
        self.generateButton.place(x=150, y=0)
        self.label2 = Label(text="--Here , your Random Name appears--")
        self.label2.place(x=70, y=30)

    def autogen(self):
        randomName = pick(names)
        self.label2.insert(INSERT, randomName)
Sign up to request clarification or add additional context in comments.

8 Comments

self.label2 is None as it is the result of Label(...).place(...).
return self.func(*args) File "nameGen.py", line 43, in autogen self.label2.insert(INSERT,randomName) AttributeError: 'NoneType' object has no attribute 'insert'
@acw1668 yes , its NoneType
@AKX i still expect an error because i believe Label dont have insert() method
Thanks a lot guys , i fixed it. It was that label error
|

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.