2

I'm trying to build a chat application with npyscreen in Python. When enter is hit on the Textfield widget, nothing happens. I'd like to make this action send the message to the server.

I've already made it working by using a MultiLineEdit widget and overriding its method

        def when_value_edited(self):

to monitor the text input changes. When a newline character was found in the current input string, it would send the message and clear the field. The problem I have with this approach is that it is a bit dirty. Apart from that I also prefer using the Textfield widget over the MultiLineEdit widget.

I tried to creating a handler in the main form, but the eval_input method is never triggered.

class MainWindow(npyscreen.FormBaseNew):
    def create(self):
        new_handlers = {
            # Set ctrl+Q to exit
            "^Q": self.exit_func,
            # Evaluate input on newline or carriage return
            curses.ascii.NL: self.eval_input,
            curses.ascii.CR: self.eval_input
        }
        self.add_handlers(new_handlers)

    def exit_func(self, _input):
        exit(0)

    def eval_input(self, _input):
        print("TRIGGERED")
        self.parentApp.msg = self.widget_input.value
        self.widget_input.value = ""

I expect the widget_input.value to change to an empty string, but nothing happens when I hit the enter key when typing in the Textfield widget. The eval_input method is never called, while the exit_func method is called as soon as I press control + q. Is it possible to do what I want to do, and if so, how?

Jurze
  • 209
  • 2
  • 11

1 Answers1

1

self.Textfield.entry_widget.handlers.update({curses.ascii.NL: self.input_send})

Oleh Myhal
  • 11
  • 1
  • 1
    Welcome to SO. It's a good idea to add explanation as to how your code answers the question. – Nick Sep 08 '20 at 21:19