-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
3
  • 1
    use self.variable within a class? Commented Feb 26, 2016 at 18:13
  • 3
    Can you give an example? I'm not quite clear what you are asking. Commented Feb 26, 2016 at 18:14
  • Tkinter is no different than anything else in python. Without seeing code it's impossible to know what you're doing wrong. Please read stackoverflow.com/help/mcve Commented Feb 26, 2016 at 21:30

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

That just outputs : PY_VAR0, PY_VAR1, PY_VAR2 for the variables
So those are the values of those variables... is that not what you expect? Look here, you might be overwriting them: stackoverflow.com/questions/31126872/…

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.