Python 从一个函数调用另一个函数中的变量

Python 从一个函数调用另一个函数中的变量,python,function,Python,Function,我有一个类,其中有两个函数 我希望从一个函数访问另一个函数中创建的变量 下面是我尝试做的一个例子: def __init__(self, parent, controller): tk.Frame.__init__(self,parent) Message1 = None Keyword1 = None Keyword2 = None Message_Ent = tk.Entry(self, textvariable = Message1)

我有一个类,其中有两个函数

我希望从一个函数访问另一个函数中创建的变量

下面是我尝试做的一个例子:

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

现在,
Message1
Keyword1
Keyword2
都是。你想让他们成为班上的佼佼者。您可以使用
self
关键字执行此操作:

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

在类中使用
self.variable
?你能举个例子吗?我不太清楚你在问什么。Tkinter和python中的其他任何东西都一样。如果看不到代码,就不可能知道自己做错了什么。请阅读以下内容:变量的输出:PY_VAR0,PY_VAR1,PY_VAR2这些是这些变量的值。。。这不是你所期望的吗?看这里,您可能正在覆盖它们: