I have this form, PasswordView, that is activated for users to fill up. The form does not have normal physical buttons. Instead, they are created programmatically as below.
done = False
count = 0
while not done:
# get user id a separate window
self.clear()
search_id = {}
alert(
content= PasswordView(item = search_id),
title="CONFIRM OPEN FORM WITH USER ID",
large=True,
buttons=[("Submit", True), ("Cancel", False)]
)
…followed by more codes
How can I set focus on ‘Submit’ so that users may just press ‘Enter’ instead of pointing the cursor and click?
What I’ve tried and what’s not working:
I kind of just guessed wildly, if I may, using the ‘self’ thingy but I know it won’t work since there are no instantiated objects. So, those were just wasted minutes.
Would greatly appreciate any tips on this issue.
Much thanks.
I think the reason it can’t with non form content is that the alert is purely synchronous and so any code the calling form runs can’t affect the internals of the alert after it launches (like for finding the elements of the built in buttons with CSS Selectors) for that we’d need an async version I think.
I can confirm that your code works…it puts the focus on the submit_button in my PasswordView form. The only issue is that the focus is temporary. Once I start entering text in the text_box for password, the focus on the button is lost.
Anyway to keep that focus steady? Possible to insert your code inside def text_box_password_pressed_enter?
Nonetheless, I do appreciate @stucork tip. I did learn how to put focus on an Anvil object like buttons. Thanks for the tip. It might come in handy sometime.
For your use case it looks like focusing the submit button is not what you want.
If your user needs to type in the text box and hit enter to submit the alert, then the pressed enter event is exactly what you want.
You could also do self.submit_button.raise_event("click") (in the pressed_enter event handler), but that’ll just call the self.outlined_button_submit_click() handler in the end anyway.
My original intention was to focus on a virtually created button, not the normal physical one. I guess that would need a different solution like that proposed by @stefano.menci using his InputBox and Alert2.
the InputBox example is the same, it focuses a physical button added to the component.
It uses anvil_extras.augment for convenience, which would look like this
from anvil_extras import augment
def form_show(self, **event_args):
self.submit_button.trigger("focus")
My understanding is that InputBox focuses the default button which you declare
It looks something like this
from anvil_extras import augment
def _set_focus(sender, **event_args):
sender.trigger('focus')
class InputBox(anvil.Component):
def show(self):
...
for button in all_buttons:
if is_default_button(button):
if not self.focus_set:
button.add_event_handler('show', _set_focus)
break
Thanks for further lessons, I have used InputBox only for creating customized alerts. There is an earlier post here from @stefano.menci about something in his InputBox that might help…that’s why I brought if up.
This is not an issue, this is how the focus works.
Having the focus on a component means the keyboard sends the input to that component.
It’s impossible to have the focus on a button, that is having the keyboard sending input to a button, while entering text in a text_box, that is having the keyboard sending input to the text_box.
Perhaps you are using the word focus, but you mean something else?
Usually these are the things to keep in mind when dealing with input on a form:
Where (most of) the keyboard input goes, this is the component with focus
What happens when you press enter, sometimes called the default or the enter component
What happens when you press esc, sometimes called the cancel component
What happens when you press shortcuts like Ctrl+S, etc., which can change when the focus changes component, even if that keystroke doesn’t go to that component
Perhaps by “focus on a button” you mean “firing that button’s click event when pressing enter while the focus is on another component”?
If that is the case, that is also one of the issues I addressed in InputBox.
Sorry, gentlemen. I had a wrong idea about ‘focus’.
I thought that after entering the password in the textbox area, it would be possible to shift the focus to the virtual button (‘Submit’) to enter/validate the password. Since I could not find a workaround from @stefano.menci InputBox/Alert2, the solution ended with normal button and its self.button_submit_click.
So, yes. I thought of ‘focus’ for the virtual button but lost it when I switched to the normal button.