What I’m trying to do:
I am trying to dismiss an alert from the code when a user triggers an action, but I am not able to do so.
What I’ve tried and what’s not working:
I have tried triggering a close event, but I am not able to get the text box to close.
I am triggering the close event when the user hits the enter key on their keyboard.
Code Sample:
from ._anvil_designer import Form1Template
from anvil import *
import anvil
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import time
class Form1(Form1Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run when the form opens.
self.a = None
def close_box(self, **event_args):
"""This method is called when the button is clicked"""
print(self.a.raise_event("x-close-alert", value=42))
self.a.hide()
def keypress(self, which, key):
print(which,key)
keydict = {
"Enter":self.close_box
}
try:
keydict[key]()
except Exception as e:
pass
def button_1_click(self, **event_args):
"""This method is called when the button is clicked"""
self.a = alert("this is an alert",dismissible=True)
Javascript for triggering the keypress function
<script>
$(document).on('keypress', (e) => {
anvil.call($('.content'), 'keypress', e.which, e.key)
})
</script>
Clone link:
https://anvil.works/build#clone:TEHLP4I3MV57AOV6=5VQQ4DVMEW53NICFFII6TCQR
Thank you so much for all of your help!