'kivy error when adding the navigationbar in matplotlib : It already has a parent

Dears, I want to display some matplotlib graphs and their matplotlib toolbars in a Kivy frame.

When the line g.graf1.add_widget(n.actionbar) is commented, the matplotlib graph is displayed as expected.

When uncommented, I got the error kivy.uix.widget.WidgetException: Cannot add <kivy.uix.actionbar.ActionSeparator object at 0x000001EC7BB1EE18>, it already has a parent <kivy.uix.actionbar.ActionOverflow object at 0x000001EC7BB1E590>

Running Python 3.6.3, Matplotlib 2.1.1, Kivy v1.10.0 on Windows 10. Here is the python and kivy code to reproduce error. Thks for your help. Rgds

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
import matplotlib
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
import matplotlib.pyplot as plt

from matplotlib.figure import Figure
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvas,\
                                                NavigationToolbar2Kivy
#create kick matplotlib grah
f=plt.figure()
topGraph=plt.subplot2grid((3,3),(0,0), rowspan=2, colspan=3)
topGraph.plot([1,2,3], [34,22,36], 'go-', label='line 1', linewidth=2)
canvas = f.canvas

class HelloWorldApp(App):
    def build(self):
        g=MyMatApp()
        n = NavigationToolbar2Kivy(canvas)
        g.graf1.add_widget(canvas)
        g.graf1.add_widget(n.actionbar) #---- ERROR it already has a parent
        return g

class MyMatApp(BoxLayout):
    def __init__(self, **kwargs):
        super(MyMatApp, self).__init__(**kwargs)
        grid = ObjectProperty(None)
        graf1 = ObjectProperty(None)
        graf2 = ObjectProperty(None)

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

.KV

<MyMatApp>:
    grid: Grid
    graf1: Graphe1
    graf2: Graphe2
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            orientation: 'horizontal'
            BoxLayout:
                id : Graphe1
                orientation: 'vertical'
            Button: 
                text: 'Middle'
            BoxLayout:
                id : Graphe2
                orientation: 'vertical'

        GridLayout:
            id: Grid
            cols: 5
            rows: 5


Solution 1:[1]

You probably have this answered by now but i just recently experienced a similar issue and I decided to full on just get the buttons out of the action view and place them as i please in my layout. Just remember that the button have to be removed from their current parent widget to be added to another widget. Hope this helps.

grid = GridLayout(cols=9)
i=0
while i < 9:
    kid = nav.actionbar.children[0].children[0]
    nav.actionbar.children[0].remove_widget(kid)
    grid.add_widget(kid)
    i+=1
mainNav.add_widget(grid)

Solution 2:[2]

The problem is that, NavigationToolbar2Kivy, only work on father layout
Example:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from matplotlib.figure import Figure
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvas,\
                                                NavigationToolbar2Kivy
self.menu=BoxLayout()
self.grid=GridLayout()
self.menu.add_widget(self.grid)
#Graph is a graphic example, like bar, plot or hist<br>
Canvas=FigureCanvasKivyAgg(graph) 
self.grid.add_widget(Canvas)`<br>
toolbar=NavigationToolbar2Kivy(Canvas)
#Well, my father layout is self.menu and isn't self.grid, then...
self.menu.add_widget(toolbar.actionbar)

enter image description here enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thembekile Mkhombo
Solution 2