Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

root_context_menu.parent is None #5

Open
loicpw opened this issue Dec 2, 2018 · 0 comments
Open

root_context_menu.parent is None #5

loicpw opened this issue Dec 2, 2018 · 0 comments

Comments

@loicpw
Copy link

loicpw commented Dec 2, 2018

Hi,

I think I found a bug in context_menu: here's the scenario:

I am creating a context menu which I'm using on nodes of a TreeView, and had a crash that appeared randomly.

I'm using the same context menu instance for all the nodes of my TreeView.

The context menu is initially added to the top layout, which is a FloatLayout object, here is the code:
(note I had to add some code to fix a weird behaviour as well, which is apprently not related, I don't know if it's because I'm not using it the right way or if it's a bug)

class TreeContextMenu(ContextMenu):
    def __init__(self, *args, **kw):
        super().__init__(*args, **kw)
        app= App.get_running_app()
        rw = app.root
        self.cancel_handler_widget = rw
        rw.add_widget(self)

        # bug workaround (otherwise keep showing the menu in the bottom) ?
        self.visible = True
        self.hide()

when calling the menu (on right click on the nodes of my treeview with super().show(*app.root_window.mouse_pos)), I had the following crash, but only in some circumstances:

[...]/context_menu.py", line 211, in show_submenu
     self.get_submenu().show(*self._root_parent.to_local(x, y))
 AttributeError: 'NoneType' object has no attribute 'to_local'

I figured out it was because root_context_menu.parent was None in get_context_menu_root_parent method of ContextMenu.

The following code seems to fix the problem, using root_context_menu.orig_parent if root_context_menu.parent is None:

def get_context_menu_root_parent(self):
        """
        Return the bounding box widget for positioning sub menus. By default it's root context menu's parent.
        """
        if self.bounding_box_widget is not None:
            return self.bounding_box_widget
        root_context_menu = self._get_root_context_menu()

        # --------------------------------------------- #
        # ORIGINAL CODE
        # --------------------------------------------- #
        #return root_context_menu.bounding_box_widget if root_context_menu.bounding_box_widget else root_context_menu.parent

        # --------------------------------------------- #
        # FIX
        # --------------------------------------------- #
        if root_context_menu.bounding_box_widget:
            return root_context_menu.bounding_box_widget
        else:
            if root_context_menu.parent is None:
                assert root_context_menu.orig_parent
                return root_context_menu.orig_parent
            return root_context_menu.parent

Thanks

@AndreMiras AndreMiras transferred this issue from kivy-garden/garden.contextmenu Dec 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant