npcole / npyscreen

Automatically exported from code.google.com/p/npyscreen
Other
481 stars 109 forks source link

Multiple objects in a box #95

Open Wieves opened 4 years ago

Wieves commented 4 years ago

Hi! I've seen old posts about putting widget in a BoxTitle (https://groups.google.com/forum/#!topic/npyscreen/0m87zQ9kdtU) Is-it possible to add multiple objects in a BoxTitle? Thanks!

LuckyCrasher commented 4 years ago

It is possibille, but I don't knwo if it is a good idea. I had to try it to, so...

class Window(npyscreen.BoxTitle):
    _contained_widget = npyscreen.DummyWidget

    def __init__(self, screen, contained_widget_arguments=None, *args, **keywords):
        super(Window, self).__init__(screen, *args, **keywords)
        self.wg_relx = self.relx
        self.wg_rely = self.rely

        if contained_widget_arguments:
            self.make_contained_widget(contained_widget_arguments=contained_widget_arguments)
        else:
            self.make_contained_widget()
        if 'editable' in keywords:
            self.entry_widget.editable = keywords['editable']
        if 'value' in keywords:
            self.value = keywords['value']
        if 'values' in keywords:
            self.values = keywords['values']
        if 'scroll_exit' in keywords:
            self.entry_widget.scroll_exit = keywords['scroll_exit']
        if 'slow_scroll' in keywords:
            self.entry_widget.scroll_exit = keywords['slow_scroll']

    def insert_widget(self, widget, **widget_arguments):
        if self._my_widgets == npyscreen.DummyWidget:
            self._my_widgets.clear()

        if widget_arguments:
            self._my_widgets.append(widget(self.parent, rely=self.wg_rely + 1, relx=self.wg_relx + 2,
                                           max_width=self.width - 4, max_height=self.height - 2,
                                           **widget_arguments))

        else:
            self._my_widgets.append(widget(self.parent, rely=self.wg_rely + 1, relx=self.wg_relx + 2,
                                           max_width=self.width - 4, max_height=self.height - 2,))

        self.wg_rely += 1

        # self.entry_widget = weakref.proxy(self._my_widgets[0])
        # self.entry_widget.parent_widget = weakref.proxy(self)

        return weakref.proxy(self._my_widgets[-1])

    def update(self, clear=True):
        if self.hidden and clear:
            self.clear()
            return False
        elif self.hidden:
            return False
        super(Window, self).update(clear=clear)
        for w in self._my_widgets:
            self.entry_widget = w
            w.update(clear=clear)

To make it work I had to subclass the Box title class. I also inserted a DummyWidget as as starting point, so we don't have an empty Box. after that it is basicly just the old init function. To make it finally work we create a new function, called insert_widged. So we can insert new widgets afterwards. the we override the update function to make it work with the modified contained widgets. IMPORTANT: the insert widget function returns a reference to the inserted widget so you can interface with it. You can now create a new widget with: window1 = self.add(Window, **kwargs) text1 = window1.insert_widget(npyscreen.FixedText, name="Test") text1.set_value("abcdefghijklmnop")

Correct me if I'm wrong or leave a comment.