Kivy Dynamic Widget - Widget resizing when children are added

by: harshal1618, 8 years ago


I am trying to write a dynamic widget which can add multiple choice question to gridlayout, which in turn will be displayed in scroll view.
Everything works correctly except for when app is run, height of widgets displayed is too big, and it comes to correct value only after window resizing. Please let me know what am I doing wrong.

#main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView

class Mcq2(GridLayout):
    def __init__(self, *args, **kwargs):
        super(Mcq2,self).__init__(**kwargs)
        self.ids.id_mcq2_Q.text = args[0]
        self.ids.id_mcq2_A1.text = args[1]
        self.ids.id_mcq2_A2.text = args[2]
        self.ids.id_mcq2_A1.group = args[3]
        self.ids.id_mcq2_A2.group = args[3]

class WrapLabel(Label):
    pass

class DisplayRoot(GridLayout):
    pass

class ScrollScreen(ScrollView):
    pass

class Mcq2App(App):
    def build(self):
        DR = DisplayRoot()
        DR.add_widget(Mcq2('Q1'*500,'A1','A2','X'))
##        DR.add_widget(Mcq2('Q2'*500,'A1','A2','Y'))
##        DR.add_widget(Mcq2('Q3'*500,'A1','A2','Z'))
        SS = ScrollScreen()
        SS.add_widget(DR)
        return SS

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


############################################

# Mcq2.kv

<Mcq2>:
    cols:3
    spacing: 3,3
##    padding: 10,10,10,10
    size_hint: 1, None
##    height: self.minimum_height
    height: id_mcq2_Q.texture_size[1]
    Label:
        id: id_mcq2_Q
        canvas.before:
            Color:
                rgba: 0.5,0.5,0.5,1
            Rectangle:
                size: self.size
                pos: self.pos
        text: "Cell1"

        size_hint: None, None
        size: root.width*0.8, self.texture_size[1]

        text_size: self.width, None
        
    CheckBox:
        id: id_mcq2_A1
        canvas.before:
            Color:
                rgba: 0.5,0.5,0.5,1
            Rectangle:
                size: self.size
                pos: self.pos
        text: "Cell2"
        group: "grp1"

        size_hint: None, None
        size: root.width*.1, 50
        
    CheckBox:
        id: id_mcq2_A2
        canvas.before:
            Color:
                rgba: 0.5,0.5,0.5,1
            Rectangle:
                size: self.size
                pos: self.pos
        text: "Cell3"
        group: "grp1"

        size_hint: None, None
        size: root.width*.1, 50

<DisplayRoot>:
    cols: 1

    spacing: 5,5
    padding: 10,10,10,10
    size_hint_y: None
    height: self.minimum_height
    on_height: print('Root.minimum_height=', self.minimum_height)





You must be logged in to post. Please login or register an account.