Kivy Python: Accordion inside an accordion, using a variable
<p>I am trying to create an accordion menu (no.1), in which there is another accordion menu (no.2).
The size of accordion no.2 will be defined by the user (an example of the outcome is shown in this image).
<a href="https://i.stack.imgur.com/VDjs6.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/VDjs6.jpg" alt="enter image description here"></a></p>

<p>The issue is that though I have managed to create a class that creates accordion no.2 following users input - I  can`t seem to find the way to display it on the screen. </p>

<p>This is my py code:</p>

<pre><code>from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.accordion import Accordion, AccordionItem
from kivy.properties import NumericProperty

wide = 0
long = 0

class AccordionClass():
    def calc(val):
        number = val
        root = Accordion(size= (200,700), pos = (50,80), orientation= 'vertical')
        for x in range(number):
            print ('x = ',x)
            item = AccordionItem(title='Title %d' % x)
            item.add_widget(Label(text='Very big content\n' * 10))
            root.add_widget(item)
        print ('END')
        return root        

class LoginScreen(GridLayout):    
    numOfStories = NumericProperty()    
    def printTxt(self, text, TextInputId):
        pass

    def addFloor(self,text):
        self.numOfStories = int(text)
        print ('self.numOfStories = ', self.numOfStories)  
        rootAc = AccordionClass.calc(self.numOfStories)
        return rootAc
    pass

class screen2(App):
    def build(self):
        self.root = GridLayout()
        return LoginScreen()

if __name__ == "__main__":
    screen2().run()  
</code></pre>

<p>and my kv code:</p>

<pre><code>&lt;TextInput&gt;:
    multiline: False
    size:150,23
    font_size: 12
    padding: [5, ( self.height - self.line_height ) / 2] 

&lt;Label&gt;:
    size:120,18
    font_size: 12
    padding: [5, ( self.height - self.line_height ) / 2]        

&lt;LoginScreen&gt;:      
    canvas:
        Color:
            rgb: (0.93, 0.93, 0.93,1)
        Rectangle:
            pos: self.pos
            size: self.size

    GridLayout:
        size:root.size
        cols:2                  
        Accordion:
            size_hint: (1.0,0.2)
            orientation: 'vertical'
            AccordionItem:                  
                title: 'Plot'
                GridLayout:                     
            AccordionItem:
                title: 'Number'
                GridLayout:
                    Label:  
                        text: "Number"
                        color: [0, 0, 0, 1]
                        pos:root.x, root.top-self.height-100                    
                    TextInput:          
                        pos:root.x+120, root.top-self.height-100                        
                        id: NumOfStories
                        on_text_validate: root.addFloor(NumOfStories.text)      
            AccordionItem:
                title: 'Another number'

        Button:
            background_color: (5,5,5,1)  
</code></pre>

<p>Any idea how to solve this issue?
Thanks</p>

<p>It isn't displaying because of you returning an instance of <code>Accordion</code> into nothing in <code>addFloor</code>/<code>calc</code> in <code>on_text_validate</code>. To create a widget you have to call <code>&lt;parent&gt;.add_widget(&lt;widget&gt;)</code>, so let's do that:</p>

<pre><code>on_text_validate: root.add_widget(root.addFloor(NumOfStories.text))
</code></pre>

<p>Then there's the thing your <code>calc()</code> is a class method for now and you either need to use <code>self</code> as an additional parameter (and have even more mess), or use the <code>@staticmethod</code> decorator, which makes the <code>calc()</code> free of class stuff and let you use it this way <code>Class.method(...)</code></p>

<pre><code>@staticmethod
def calc(val):
</code></pre>

<p>After that a new <code>Accordion</code> will show up, but the sizing and positioning is up to you. Also, by default there's probably no background for that widget, so you'll end up with putting it there via <a href="https://kivy.org/docs/api-kivy.graphics.html" rel="nofollow">canvas instructions</a>.</p>

python kivy kivy-language