Accessing a variable on another form without using get_open_form or self.parent

Thank you to everyone who contributed to this discussion!

When I made the original post, my issue was understanding how to pass variables between 2 forms in general. I tried the first solution posted, which worked (thanks @stefano.menci)

My problem then became ‘how will the component on Form 1 know that a component on Form 2 has been clicked’, as what I was trying to do was allow the user to click a button on Form 2 (row for repeating panel) which would then set some text on Form 1.

This would require something on Form 1 to be actively ‘listening’ for things happening on form 2.

The first solution I tried was using the Anvil Extras Messaging Module suggested by @owen.campbell, and it worked.

Here are the steps I followed using the messaging module:

  1. Create a client module called ‘common’ with this code:

from anvil_extras.messaging import Publisher
publisher = Publisher()

  1. On Form 2 (the form where the user would click the button), set up the ‘sender’:
from .common import publisher

def add_button_click(self, **event_args):
  text = self.item['Brand_Name'] # This is a piece of text taken from the data table
  publisher.publish(channel='general', title=text)
  1. On Form 1 (the form where the user would need to see the text appear after clicking the button) set up the ‘receiver’:
from .common import publisher

# In the Form's __init__
publisher.subscribe(channel='general', subscriber=self, handler=self.text_message_handler)

# Then, make a function within Form 1, this effectively listens for the button being clicked on the other form:

def text_message_handler(self, message):
  self.text_label.text = message.title

My app now works as desired and no data-grids were harmed in the process.

Thanks all!

Richard

4 Likes