Calling functions between forms that have no parent relationship

I have two forms that exist at the same hierarchy (no parent relationship between them). I am able to have FormA open FormB while passing parameters from FormA at FormB’s init stage. However, after doing some work at FormB, I need a button there on FormB that will return variables back to a function (let’s call the function processBResult() )within FormA that isn’t included at FormA’s init level.

I was hoping that it would be as simple as including within FormB a button click action line that would read:
open_form(FormA.processBResult(insert variables here))
ie taking the format:
open_form(FormName.FunctionName(variables to pass))

however, the ability to specify the function name along with the form name in the open_form command doesn’t seem to work.

All of the forum threads I’ve seen about this issue involve solutions that are:
a) assume that there is a parent<>child hierarchy between FormA and FormB (which isn’t my case), or
b) assume that there is some kind of integrated navigation to the forms where there is a Master Form calling both FormA and FormB into it as objects.

I’m including the following steps below as a way to clearly reference where/when/what to do if anyone has suggestions for me.

Thanks so much for you consideration.

Step1: run function A1 within FormA
Step2: run function A2 within FormA that opens FormB at init state and passes variables to it
Step3: run function B1 in FormB that processes the variables received from Step2
Step4: run function B2 in FormB that returns to FormA again with variables but not at the init level.

I did not read thoroughly through the post, but I did read the concise steps at the bottom. Maybe this will help a bit.

This you know how to do already. Great.

# in formA
def A2():
  open_form('formB', foo=bar)
# in formB init
bar=properties['foo']
self.tag.stuff_saved_for_later=bar

def B1():
 print(self.tag.stuff_saved_for_later)
# in formB
def B2():
 # using tag here but can pass as standard argument to B2
 # my_var can be accessed in formA's init similar to above
 open_form('formA', my_var=self.tag.stuff_saved_for_later) 
2 Likes

Note: the first parameter to open_form() can be either

  1. an existing Form object, in which case that object becomes the current form, or
  2. a string (the name of a Form class), in which case, a new instance of that class is created, and becomes the current form. This does not destroy the old instance!

The current form is then shown in the browser.

It seems to me that your instance of FormA already exists, and your instance of FormB should communicate with it. In that case, the FormA instance can simply pass itself (self) to the instance of FormB as just another variable (in step 2). E.g., in FormB's __init__() function:

    __init__(self, calling_form, **properties):
        self.calling_form = calling_form
    ...

The FormB instance then has full access to the (passed) FormA instance, as self.calling_form.

  • It can call FormA's functions as it sees fit: self.calling_form.desired_function(args).
  • For Step 4, it can also switch the display back to the calling form: open_form(self.calling_form)
2 Likes

Excellent! It worked perfectly using your instructions/insights below. The concept I wasn’t getting was that the state of the calling form wasn’t changed (or rather, it was still accessible) when navigating away to the second form. Previously, I wasn’t passing the FormA “self” from FormA to FormB. Therefore, when I was returning back to FormA from FormB, it was returning me back to FormA as if I was entering it fresh.

Thanks so much for the insights and detailed explanation. Instead of it just getting it working, I feel like I have a keener sense of why it’s working.

It seems to me that your instance of FormA already exists, and your instance of FormB should communicate with it. In that case, the FormA instance can simply pass itself ( self ) to the instance of FormB as just another variable (in step 2). E.g., in FormB 's __init__() function:

    __init__(self, calling_form, **properties):
        self.calling_form = calling_form
    ...

For Step 4, it can also switch the display back to the calling form: open_form(self.calling_form)

1 Like