-1

I have a class, in which two functions are present.

I wish to access a variable created within one function from the other.

Here is an example of what I'm trying to do:

def __init__(self, parent, controller):
    tk.Frame.__init__(self,parent)

    Message1 = None
    Keyword1 = None
    Keyword2 = None


    Message_Ent = tk.Entry(self, textvariable = Message1)
    Key1_Ent = tk.Entry(self, textvariable = Keyword1)
    Key2_Ent = tk.Entry(self, textvariable = Keyword2)


def Main_Cipher(*args):
    #Need to use these variables:
    #Message1
    #Keyword1
    #Keyword2
River
  • 8,585
  • 14
  • 54
  • 67
Smiffy
  • 1
  • 1

1 Answers1

1

Right now Message1, Keyword1, and Keyword2 are local variables. You want to make them instance variables of the class. You do this using the self keyword:

def __init__(self, parent, controller):
    tk.Frame.__init__(self,parent)

    self.Message1 = None
    self.Keyword1 = None
    self.Keyword2 = None


    Message_Ent = tk.Entry(self, textvariable = self.Message1)
    Key1_Ent = tk.Entry(self, textvariable = self.Keyword1)
    Key2_Ent = tk.Entry(self, textvariable = self.Keyword2)


def Main_Cipher(*args):
    #these are now accessible here:
    print self.Message1
    print self.Keyword1
    print self.Keyword2
River
  • 8,585
  • 14
  • 54
  • 67
  • That just outputs : PY_VAR0, PY_VAR1, PY_VAR2 for the variables – Smiffy Feb 27 '16 at 17:16
  • So those are the values of those variables... is that not what you expect? Look here, you might be overwriting them: http://stackoverflow.com/questions/31126872/python-tkinter-check-button-printing-py-var0 – River Feb 27 '16 at 17:37