I have a main form with sub forms that requests a user log in, if they are not already logged in.
On the UserLogin sub form I have a button click event
def button_1_click(self, **event_args):
user = anvil.users.login_with_form(allow_cancel=True)
get_open_form().set_account_state(user)
print('you are now logged in')
user = the_user()
print(user)
Quotation.self.card_travellers.visible = True
Quotation.self.card_user.visible = False
pass
But these lines are not doing what I want
user = the_user()
print(user)
Quotation.self.card_travellers.visible = True
Quotation.self.card_user.visible = False
I want to print the now logged in user
I want to make a sub form visible Quotation.self.card_travellers.visible = True
and I want to hide the login sub form as its no longer required Quotation.self.card_user.visible = False
you’re already setting local variable user. So is
really necessary? You run the risk of losing user’s previous, known value here.
the_user isn’t a standard Anvil function, so we can’t guess what it will do or return. We don’t even know the type of value it is expected to return. But since
isn’t doing what you expect, I suggest you look at your definition of the_user to see what it is actually doing, as opposed to what you want it to do. That’s probably where your problem lies.
and now I get this error AttributeError: 'HomeForm' object has no attribute 'card_travellers'
Homeform is essentially my front page and all other pages open within that form, depending on the menu link clicked, so yes that is the open form, just not the one I want it to look for those cards in.
How can I get it to look for Quotation and not Homeform?
You need to execute that code in a form that has a reference to the instance of the Quotation form. It sounds like right now it’s being executed in a subform. The clone link from your other post is not working again, so I can’t give any specifics, but in general:
If you have form A that contains subforms B and C, and you want subform B to affect the visibility of subform C, what I’d do is have form A register an event on subform B. Then subform B can raise that event, and the code that actually handles the event will be in form A, where you have the references to subforms to work with.
The following is vague since I can’t look at the actual code, so take it as an algorithm more than code:
# Form A, wherever subform B is created and added
self.b = B()
self.add_component(self.b)
self.b.set_event_handler('x-user-logged-in', self.user_logged_in)
# Also in Form A
def user_logged_in(self, **event_args):
self.b.visible = False
self.c.visible = True
# In Form B
def button_1_click(self, **event_args):
user = anvil.users.login_with_form(allow_cancel=True)
get_open_form().set_account_state(user)
self.raise_event('x-user-logged-in')
I’d need to know more about the structure of your app to say for sure. But let’s say you have a Quotation object in your HomeForm. It’s possible that you added it (with code) to your HomeForm without giving it a name. (If so, add a line like self.quotation_page = Quotation() to give it a name.) Once you can access it from the HomeForm as something like self.quotation_page, then from the UserLogin sub form you would be able to access it as get_open_form().quotation_page. So the card_user could be accessed as get_open_form().quotation_page.card_user, and so on.
For alternative approaches (and examples), see this post and the links therein: