Text Box "pressed_enter" from code?

I’m trying to define a “pressed_enter” event for a text box, but I keep getting the following error:

AttributeError: 'TextBox' object has no attribute 'pressed_enter'

I have instantiated a TextBox object and tried setting the event handler using object.set_event_handler, which is what causes the above error.

The text box is generated by a previous function (not dragged and dropped from components), so I need to create the event directly from code rather than from the GUI event handler. I’ve read the documentation thoroughly but I feel I must be missing something (I am very new to Anvil).

What am I doing wrong?

The code should look something like

tb = TextBox()
tb.set_event_handler('pressed_enter', some_function)
# some function is any function

The error suggests you did something like
tb.set_event_handler(tb.pressed_enter, some_function)

Any more code snippets/clone link would be helpful to narrow down the problem

Thank you @stucork, I think you nailed the problem.

My code looks like this:

counter = 0  
    while counter < 5:
      cp = ColumnPanel(role = "card")
      l = Label(text = "The current count is {}.".format(counter))
      l2 = Label(text = "Choose a number.")
      tb = TextBox(type = "number")
      def get_num(self):
        number = tb.text
        return number
      tb.set_event_handler("pressed_enter", get_num(self))
      self.add_component(cp)
      cp.add_component(l)
      cp.add_component(l2)
      cp.add_component(tb)
      counter += get_num(self)

From this, I now get the error TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType' at Form1, line 26 (the line in question is counter += get_num(self)).

All I’m trying to do is test adding elements via a while loop: what it should do is produce the first card with the respective components, and then produce the next based on the number the user enters into the tb text box, until the counter reaches 5. I’m having trouble with the dependency aspect.

I’d definitely recommend posting a clone link if you can. It’s not completely clear what the code is trying to do.

Some pointers:

  • If a TextBox type is number then when it is empty its text property is None.
counter += get_num(self) or 0 # maybe
# or
num = get_num(self)
counter += 0 if num is None else num # inline if else
  • you’re not setting the event handler to a function - you’re calling a function and then setting the event_handler to the return value
      def get_num(self):
        number = tb.text
        return number
      event_handler = get_num(self) # calls the function
      print(event_handler) # None
      tb.set_event_handler("pressed_enter", event_handler)
      # you've just set the event_handler to None

The above code should look something like

      def get_num(**event_args):
        number = tb.text
        return number
      tb.set_event_handler("pressed_enter", get_num) 

But it’s not clear from that code what action you want to happen when the user presses enter.
Currently nothing in the UI will really change.

  • The code is set up for an infinite loop. Since the counter is never incremented as the tb.text property never changes.