# Passing reference to component to a window

**URL:** <https://forum.inductiveautomation.com/t/passing-reference-to-component-to-a-window/59368>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [May 9, 2022, 5:53pm UTC](https://forum.inductiveautomation.com/t/passing-reference-to-component-to-a-window/59368 "2022-05-09T17:53:29Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![gdube](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gdube/32/41701_2.png) [@gdube](https://forum.inductiveautomation.com/u/gdube)\
**Post date:** [May 9, 2022, 7:47pm UTC](https://forum.inductiveautomation.com/t/passing-reference-to-component-to-a-window/59368/3 "2022-05-09T19:47:30Z")

</div>

I'm copy pasting my personal notes, but perhaps you'll find them useful.  
Here:

## Using callbacks in Vision

Window - Caller

- Can be in the script of a button.
- The callback can reference `event` or `self` depending on the context in which this script is running (within a custom method, or a component event).

```python
def callback():
	print 'hey'

window = system.nav.openWindowInstance('Popups/popupName')
window.putClientProperty('callback', callback)
system.nav.centerWindow(window)

```

Popup - Callback

```python
window = system.gui.getParentWindow(event)
callback = window.getClientProperty("callback")
callback()

```

Questions

- What happens if client property isn't defined? I think it just returns None.
- What happens if calling window isn't opened anymore?
  - Use weakref(s) if that's a concern.

### Using weakref

Note:  
Weakref is a python module to add to folder `user-lib\pylib` of Ignition:  
It avoids breaking garbage collection in some cases.

```python
# -----------------
# DUBEG:
# For whatever reason, the weakref must be created
# within a built-in component event (eg. propertyChange, actionPerformed), 
# and not within a custom/extension method, otherwise it won't work (weakref returns None).
# Not sure why, but probably related to the way extension methods are handled.
# You can always pass the callback function as an argument to a custom method, and that will work.
# -----------------
import weakref

textbox = event.source.parent.getComponent('tbResult')
textboxRef = weakref.ref(textbox)

def callback():
	o = textboxRef()
	if o is not None:
		o.text = "Callback :)"

window = system.nav.openWindowInstance('Popups/ppuConfirmAction')
window.putClientProperty('callback', callback)
system.nav.centerWindow(window)

```

---

_[View the full topic](https://forum.inductiveautomation.com/t/passing-reference-to-component-to-a-window/59368)._
