Problem with textlabel from another form

What I’m trying to do:
use a textlabel from another form
What I’ve tried and what’s not working:
UnboundLocalError: local variable ‘Table’ referenced before assignment
Code Sample:

# this is a formatted code snippet.
# paste your code between ``` 

Clone link:
share a copy of your app

the area for error is unbounded.

It would be really helpful to get a code snippet or copy of your application and more context on what you’re trying to do.

3 Likes

i think it’s a inheritance problem
i have 2 independant forms in the same app
i need to use a component from form 1 in the form 2 is that possible ?

i have tried this code
from …Form1.ItemTemplate1 import *
in the second form
but didnt work

It is very possible to do this. Again, a code snippet or clone would be helpful in identifying your issue. You can clone applications to share, here is a link on how to do so:

The import alone will not allow you to interact with Form1 from Form2. You need to instantiate Form1 somewhere in your application. Then you can interact with that instantiation of Form1 in Form2, given an appropriate link between the two forms is made.

5 Likes

https://anvil.works/build#clone:45RLGY4HWG2FFIRQ=5A4AJOOF3P7HGSU42Y6TUHMP

1 Like

I know understand your issue.

What you are doing:

You open an instance of Form1 with a repeating panel. You want to set the Table variable to be set to which ever number is clicked on Form1.

You then want Form2 to open and have access to that Number that was selected.

The Problem:

You are setting the Table variable to a local variable only available within the method button_1_click in the repeating panel of Form1.

This means that variable is only available within the scope of that method. As soon as you exit that method, the variable no longer exist. Even within Form1 or the repeating panel itself.

How can you fix it?

There are multiple ways you can fix it.

I would say you should spend some time reading up on classes, methods, and scope. This will help you understand how to get these two forms to communicate and is the core issue. It will also prevent you from repeating this mistake and save a lot of headache (I have definitely given myself too much headache by not reading up enough).

Either way, I don’t wanna leave you without some solution to this specific issue so here it is:

I would create a parent form , lets call it “Base” where you can set class variables that will live outside the scope of your methods. Then you can open your Form1/ Form2 in your “Base” form as components. To do this you can drag them onto the form or add them to the form via code using <column panel component>.add_component(Form1()) . From there, you can figure out how to set the class variables of the “Base” when ever a repeating panel row is clicked.

Clone to get you started:

https://anvil.works/build#clone:FUCXN4ZKE3UKLLCV=5FLLDVRTHJCNKHK2PSHGPYJ3

It is far from perfect or complete, but I wanted too give you an idea of what I was talking about.

hint: I would use a publisher to publish when a component is clicked to and pick that up in your base form. anvil extras is a great dependency you can add to your code to make your life easier and has the publisher library. here is a link to that:

All the instructions on how to use it can be found in the documentation or even by searching in this forum.

Cheers!

1 Like

i didnt understand you very well,
but after importing the form like this :
from …Form1 import Form1

can i use a function or what ever in the second form for exemple :
Table=Form1().self.button_1.text
or
Table=Form1().get_components(self.button_1.text)
to get the number that i clicked from the form1 !

I think to further understand you should spend an hour or so understanding classes and scope.

You cannot do this. the self.button_1 you are trying to access is in a repeating panel in form 1.

self.form_1 = Form1() #creating an instance of Form1

for row in self.form1.repeating_panel_1.get_components():
    print(row.button_1.text)

This will print every button text . You can try that and play with it to understand the structure of your app and how the components play with each other.

I know this isn’t the answer you want to hear, but your understanding of how the different classes and variables interact is the issue. It’ll save you time and resolve your issue if you spend some time reviewing python, in particular , classes and scope.

I don’t want to leave you hanging, but I wont be responding further.

Hope this helps!

I’ll add

1 Like
 def button_1_click(self, **event_args):
    from ...Form1.ItemTemplate1 import ItemTemplate1
    from ...Form1 import Form1
    selim = ItemTemplate1()
    form = get_open_form()
    numtable=selim.Effacer.text
    Commander=self.button_1.text
    app_tables.commande.add_row(created=datetime.now(),Commander=self.button_1.text,Table=numtable)

I dont have warnings anymore but the variable Table didnt get write in the database

i hope that what i’m trying to do is just for paid plan
please someone tells me if i’m wrong if i want to inherit a variable in the free plan

If you want a variable to be accessible from two forms, then you need to store it outside of the two forms.

These two posts talk about different approaches to this problem:

1 Like