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)
label2ceases to exist as soon asmyElements()returns.autogen()cannot possibly access it, because there's nothing to access. Store it as an instance attribute, instead.self.label2=Label(...)and thenself.label2.insert(...)work?self.label2 = Label(...)thenself.label2.place(..)then sayself.label2.insert(..)but i believeLabeldoes not haveinsert()method