2

I am trying to change Boxlayout height dynamically. There will an empty Boxlayout at the beginning, then with a ToggleButton I will add Buttons on that Boxlayout vertically. But this Boxlayout locates in Gridlayout which has ScrollView. And the number of buttons I will add will change, so the height of the box layout should change too. But it doesn't change. Could you please help.

Here is my main.py:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.spinner import Spinner
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
from kivy.properties import ListProperty
from collections import OrderedDict

data1=["mother","father","son"]
data2=["uncle","aunt","grandfather"]
data3=["jack","mike","simon"]
data4=["1898","1975","1985","1885"]

class MainWidget(Widget):

    an0=tuple(list(OrderedDict.fromkeys(data1)))
    cal5= ObjectProperty()

    def btn10(self,text):
    
        if self.cal5:
            self.cal5.parent.remove_widget(self.cal5) #EVERY TIME I NEED TO REMOVE IT BECAUSE MY BUTTON QUANTITIY AND TEXT WILL CHANGE DUE TO TOGGLE BUTTON SELECTION
        self.cal5 =ModelSpecifications()

        a=data2
        b=data3
        c=data4

        mi=[]
        n=0

        while n < len(a):
            aba=(str(a[n])+"\n"+str(b[n])+"\n"+str(c[n]))
            mi.append(aba)
            n+=1

        for i in mi:
            self.b1=Button(text=str(i),size_hint=(1,None),height="10dp")
            self.cal5.add_widget(self.b1)
        self.ids.scd.add_widget(self.cal5, index=3) #I ADD THE BUTTONS IN HERE

class SecondPage(ScrollView):
    pass

some_number=120

calculated_height=(str(some_number)+"dp") #<------I CAN CHANGE THE HEIGHT AT THE BEGINING BUT IT DOSENT CHANGE THE HEIGHT DYNAMICALLY

class ModelSpecifications(BoxLayout): 
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.size_hint=(1,None)
        self.height=calculated_height 

class Calculation(GridLayout):
    pass   

class MyApp(App):
    pass

MyApp().run()

And here is my my.kv file:

#:import C kivy.utils.get_color_from_hex


MainWidget:
<MainWidget>:
    ScreenManager:
        id: scmanager
        size: root.width, root.height
        Screen:
            id: scndpage
            name: "second"
            SecondPage:
                Calculation:
                    id:scd            
                    cols:1 
                    height: self.minimum_height 
                    row_default_height: "70dp" #<---THIS HEIGHT MUST BE SPECIFY BECAUSE OF SCROLLWIEW
                    size_hint_y: None
                    spacing:"10dp"
                    canvas.before:
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    BoxLayout:

                        size_hint: 1, None
                        height: "50dp"
                        
                        pading:"10dp"
                        spacing:"10dp"
                        orientation: "vertical"
                        BoxLayout:
                            orientation: "horizontal"
                            Label:
                                text:"Name:"
                                color: 0,0,0,1
                            TextInput:
                                text:"---"
                                color: 0,0,0,1
                            Label:
                                text:"Surname:"
                                color: 0,0,0,1
                            TextInput:
                                text:"-----"
                                color: 0,0,0,1
                    BoxLayout:
                        id:scdd
                        size_hint: 1, 1
                        height: "100dp"
                        orientation: "vertical"
                        BoxLayout:
                            size_hint: 1, None
                            height: "50dp"
                            orientation: "horizontal"
                            Label:
                                text: " Sellection:"
                                color: 0,0,0,1
                            Spinner:
                                text: 'Home'
                                values: root.an0
                                on_text: app.root.btn10(self.text)                                       
                    Button:
                        text:" Calculate"

                    Button:
                        text:"Sellect"
                    
                    Button:
                        text:"Back"

    
<ModelSpecifications>:      #<---------HOW CAN I DYNAMICALLY CHANGE THE HEIGHT OF THIS WIDGET ?
    id:anss                
    pading:"10dp"
    spacing:"10dp"
    orientation: "vertical"
    canvas.before:
        Color:
            rgba: (C('EBE4E4'))
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [15]

I want to change the height in the python script because I will get the information from another function as variable.

Please run the file. Looking forward to your comments.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

0 Answers0