Update TitleSelectOne values list based on selected value from another TitleSelectOne widget

376 views
Skip to first unread message

Miroslaw Malinowski

unread,
Jul 14, 2016, 4:00:26 AM7/14/16
to npyscreen
Hi

I would like to update values list of TitleSelectOne widget based on selected value from another TitleSelectOne widget. I've tried to use value_changed_callback option but no luck so far.
I don't know if the list is not updated or maybe the form is not refreshed with new values.

Could anyone tell me what I'm missing, the code I've got at the moment below.

import npyscreen

def update_product_options(widget):
    widget.parent.parentApp.getForm('MAIN').product_options = ['NPSA','NPSHH']
    widget.parent.parentApp.getForm('MAIN').display()

class UnipatchMainForm(npyscreen.Form):

    def afterEditing(self):
        self.parentApp.setNextForm(None)

    def create(self):
#Add widgets to collect info
        self.product_options = ['...']
        self.product_name = self.add(npyscreen.TitleSelectOne, scroll_exit=True, max_height=5, name='Product:', values = ['Sonet', 'Durabill', 'Gina','Clava'],value_changed_callback=update_product_options)
        self.product_value = self.add(npyscreen.TitleSelectOne, max_height=5,scroll_exit=True,name='Product Value:', values=self.product_options)

class UnipatchApp(npyscreen.NPSAppManaged):
    def onStart(self):
        self.addForm('MAIN', UnipatchMainForm, name='Unipatch')

if __name__ == '__main__':
    UnipatchApp = UnipatchApp().run()


Thanks

Nicholas Cole

unread,
Jul 14, 2016, 4:21:21 AM7/14/16
to npys...@googlegroups.com
That may be a bug. I'll look this evening!
> --
> You received this message because you are subscribed to the Google Groups
> "npyscreen" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to npyscreen+...@googlegroups.com.
> To post to this group, send email to npys...@googlegroups.com.
> Visit this group at https://groups.google.com/group/npyscreen.
> For more options, visit https://groups.google.com/d/optout.

Miroslaw Malinowski

unread,
Jul 14, 2016, 7:15:27 AM7/14/16
to npyscreen
Thanks, 
When I've been debugging it using PyDev debugger it looks like TitleSelectOne.value is not a pointer to my  self.product_options list, 
it looks more like variable "value" has been created based on initial values of my  self.product_options list but as it's not a pointer I can't update the list.

I can confirm it by changing my function to update the values list directly
def update_product_options(widget):
    widget.parent.parentApp.getForm('MAIN').product_value.values = ['NPSA','NPSHH']
    widget.parent.parentApp.getForm('MAIN').display()


I also have another question, how I can do a callback based on selected option, as at the moment the callback function is called every time I move between the values, I would like it to be called only when value is selected.

Thanks

Miroslaw Malinowski

unread,
Jul 14, 2016, 9:38:36 AM7/14/16
to npyscreen
I found work around the problem by updating widget.parent.parentApp.getForm('MAIN').product_value.values with deepcopy.
I tough it would be good to post example so other can see my solution if they have a similar problem, 
I had one more very minor problem when value is set to list with single value, it took me a while to figure it out.

def update_product_options(widget):
    value = widget.value
    values = []
    if widget.value:
        value = widget.value[0]
        if value == 0:
            values = ['7.4.0','7.5.0']
        elif value == 1:
            values = ['4.0.0','4.1.0']
        elif value == 2:
            values = ['1.2.0','1.3.0']
        elif value == 3:
            values = ['2.2.0','2.3.0']
        widget.parent.parentApp.getForm('MAIN').product_value.values = copy.deepcopy(values)
        widget.parent.parentApp.getForm('MAIN').display()
Reply all
Reply to author
Forward
0 new messages