# How can i access to an array returned by other function?

**URL:** https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274
**Category:** Python Help
**Created:** [April 25, 2022, 11:00pm UTC](https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274 "2022-04-25T23:00:44Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discuss.python.org/u/Rui)
#### Post date: [April 25, 2022, 11:00pm UTC](https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274/1 "2022-04-25T23:00:44Z")

</div>

I have this function to open two arrays of a txt file and create a table and an #embedded graph in root. The problem is that i cannot access the matrix returned by this function in order to use it in a different function:

```auto
def open_txt2():
    text_file=filedialog.askopenfilename(initialdir="C:/Macintosh HD/Utilizadores/Python/Visuall Studio/", title="Open text file", filetypes=(("Text Files", "*.txt"),))

    data=np.loadtxt(text_file)
    Psi=data[:,0]
    Del=data[:,1]

    fig=plt.figure()  
    plt.plot(Psi,Del,'bo', markersize=3)
    plt.ylabel(u'\u0394 /°')
    plt.xlabel(u'\u03A8 /°')
    plt.legend(['Experimental'])  

    PlotFrame=tk.Frame(f23, width=500, height=300)
    PlotFrame.grid(row=0, column=1)
    canvas = FigureCanvasTkAgg(fig, master=PlotFrame) # A tk.DrawingArea.
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    toolbar = NavigationToolbar2Tk(canvas, PlotFrame)
    toolbar.update()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    datatable=np.column_stack((Psi,Del))
    dataset = pd.DataFrame({'Psi': datatable[:, 0], 'Del': datatable[:, 1]})
    table=Table(f21b,column=0,dataframe=dataset, width=200, sticky='w')
    table.show()
    return datatable

```

I just can access it if i open in main window like this:

```auto
a=open_txt2()
PSIe=a[:,0]
DELe=a[:,1]

```

Thank you!

---

<div class="post-metadata">

### Author: ![zafuru](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zafuru/32/6997_2.png) [@zafuru](https://discuss.python.org/u/zafuru)
#### Post date: [April 25, 2022, 11:22pm UTC](https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274/2 "2022-04-25T23:22:45Z")

</div>

What happens if you have on function to load the data and another one to display it? Something like

```auto
def open_txt2():
    text_file=filedialog.askopenfilename(initialdir="C:/Macintosh HD/Utilizadores/Python/Visuall Studio/", title="Open text file", filetypes=(("Text Files", "*.txt"),))

    data=np.loadtxt(text_file)
    Psi=data[:,0]
    Del=data[:,1]

    datatable=np.column_stack((Psi,Del))
    return datatable

```

And then another function like

```auto
def show_matrix(datatable):
    fig=plt.figure()  
    plt.plot(Psi,Del,'bo', markersize=3)
    plt.ylabel(u'\u0394 /°')
    plt.xlabel(u'\u03A8 /°')
    plt.legend(['Experimental'])  

    PlotFrame=tk.Frame(f23, width=500, height=300)
    PlotFrame.grid(row=0, column=1)
    canvas = FigureCanvasTkAgg(fig, master=PlotFrame) # A tk.DrawingArea.
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    toolbar = NavigationToolbar2Tk(canvas, PlotFrame)
    toolbar.update()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    dataset = pd.DataFrame({'Psi': datatable[:, 0], 'Del': datatable[:, 1]})
    table=Table(f21b,column=0,dataframe=dataset, width=200, sticky='w')
    table.show()

```

And then pass it like

```auto
a = open_txt2()
show_matrix(a)

```

Would this work?

---

<div class="post-metadata">

### Author: ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discuss.python.org/u/Rui)
#### Post date: [April 25, 2022, 11:37pm UTC](https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274/3 "2022-04-25T23:37:53Z")

</div>

> [@zafuru](#):
>
> ```auto
> a = open_txt2()
> show_matrix(a)
> 
> ```

I want to open an array with a button an d the i want to have this array available to be used in other function. With this :  
a = open\_txt2()  
show\_matrix(a)

It will initialize the opening of txt when start the program and i want to do it with a button. Thanks for your time!

---

<div class="post-metadata">

### Author: ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discuss.python.org/u/Rui)
#### Post date: [April 25, 2022, 11:42pm UTC](https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274/4 "2022-04-25T23:42:33Z")

</div>

I want to open a txt file and generate an array with a button command and then i need this array for a different function

---

<div class="post-metadata">

### Author: ![zafuru](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zafuru/32/6997_2.png) [@zafuru](https://discuss.python.org/u/zafuru)
#### Post date: [April 25, 2022, 11:47pm UTC](https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274/5 "2022-04-25T23:47:23Z")

</div>

[Would something like this work?](https://stackoverflow.com/questions/26607268/simplest-way-to-open-a-file-in-tkinter)

And your `openfile` function would be `open_txt2`

---

<div class="post-metadata">

### Author: ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discuss.python.org/u/Rui)
#### Post date: [April 26, 2022, 12:25am UTC](https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274/6 "2022-04-26T00:25:16Z")

</div>

Yes. And the use the array from the txt file in a different function

---

<div class="post-metadata">

### Author: ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discuss.python.org/u/Rui)
#### Post date: [April 26, 2022, 12:26am UTC](https://discuss.python.org/t/how-can-i-access-to-an-array-returned-by-other-function/15274/7 "2022-04-26T00:26:29Z")

</div>

Yes. And then need to use the array from the txt file in a different function
