2022-04-26

how to get focus in TitleText input from npyscreen?

I need to get the focus back to the first input after write and press the enter key in another input. For instance, after enter a value in myPrecio input and press enter i need to get the focus back to myCodigo input, how can i achieve this?


import npyscreen


class MyGrid(npyscreen.GridColTitles):
    # You need to override custom_print_cell to manipulate how
    # a cell is printed. In this example we change the color of the
    # text depending on the string value of cell.
    def custom_print_cell(self, actual_cell, cell_display_value):
        if cell_display_value =='FAIL':
           actual_cell.color = 'DANGER'
        elif cell_display_value == 'PASS':
           actual_cell.color = 'GOOD'
        else:
           actual_cell.color = 'DEFAULT'




class nuevoIngreso(npyscreen.FormBaseNew):
    def afterEditing(self):

        self.parentApp.setNextForm(none)


    def create(self):
        self.myDepartment = self.add(npyscreen.TitleSelectOne, scroll_exit=True, max_height=3, name='Departmento', values = ['M', 'C', 'L'])
        self.myCodigo = self.add(npyscreen.TitleText, name='CODIGO: ')
        self.myDescripcion = self.add(npyscreen.TitleText, name='DESCRIPCION: ')
        self.myKit = self.add(npyscreen.TitleText, name='UN/KIT: ')
        self.myPrecio = self.add(npyscreen.TitleText, name='$/UN')
        self.myGrid = self.add(MyGrid,select_whole_line = True,editable = False)
        # Adding values to the Grid, this code just randomly
        # fills a 2 x 4 grid with random PASS/FAIL strings.
        self.myGrid.values = []
        for x in range(3):
            row = []
            for y in range(4):
                if bool(random.getrandbits(1)):
                    row.append("PASS")
                else:
                    row.append("FAIL")
            self.myGrid.values.append(row)




class MyApplication(npyscreen.NPSAppManaged):
    def onStart(self):
        F=self.addForm('MAIN', nuevoIngreso, name='Nuevo Ingreso')
        # A real application might define more forms here.......



if __name__ == '__main__':
    TestApp = MyApplication().run()

UPDATE: after some test, i added to my code:

  self.myPrecio.entry_widget.handlers.update({curses.ascii.CR: self.input_send})

At the end of def Create(self) and i added this function to attach bind to enter key on TitleText widget:

    def input_send(self, _input):

        #self.myCodigo.editing = False
        #self.myDescripcion.editing = False
        #self.myKit.editing = False
        #self.myGrid.editing = False
        #self.myPrecio.editing =
        self.display()
        self.editw=1
        self.myPrecio.editing = False
        self.editing = False
        self.edit()

Now i can set focus on field myCodigo, but the cursor is also displayed on field myPrecio as follow:

enter image description here



1 comment: