Search code examples
pythonkivyheightwidth

can't force defualt size in kivy python


I have been following a python kivy tutorial and I am making this simple app that displays some text on the screen and want to make every except the button smaller in the app but

self.top_grid = GridLayout(row_force_default=True,row_default_height=100,col_force_default=True,col_default_width=100)

I am not sure what to do,Thanks

CODE:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class MyGridLayout(GridLayout):
    #init infinete keyword
    def __init__(self,**kwargs):
        #call grid layout constructor
        super(MyGridLayout,self).__init__(**kwargs)
        self.cols = 1       
        self.top_grid.cols = 2
        self.press_count = 0
        self.top_grid = GridLayout(row_force_default=True,row_default_height=100,col_force_default=True,col_default_width=100)
        
        
        self.top_grid.add_widget(Label(text="name: ",font_size=30))
        self.name = TextInput(multiline=False)
        self.top_grid.add_widget(self.name)
        
        
        self.top_grid.add_widget(Label(text="age: ",font_size=30))
        self.age = TextInput(multiline=False)
        self.top_grid.add_widget(self.age)
        
        
        self.top_grid.add_widget(Label(text="how are you feeling: ",font_size=30))
        self.feel = TextInput(multiline=False)
        self.top_grid.add_widget(self.feel)
        
        
        self.add_widget(self.top_grid)
        #button
        self.button = Button(text="go!!!",font_size=32,size_hint_y=None,height=(100-self.press_count))
        self.button.bind(on_press=self.press)
        self.add_widget(self.button)
        
    def press(self, instance):
    
        name = self.name.text
        age = self.age.text
        feel = self.feel.text
        self.add_widget(Label(text=f"you are {name} and you are {age} and you are feeling {feel}",font_size=15))
        #print(f"you are {name} and you are {age} and you are feeling {feel}")
        
        
        self.name.text = ""
        self.age.text = ""
        self.feel.text = ""
        
        
        self.press_count +=10
        if not self.press_count == 20:
            self.button.height=(100-self.press_count)
            
            
class MyApp(App):
    def build(self):
        return MyGridLayout()
        
        
if __name__ == "__main__":
    MyApp().run()

Solution

  • Good day. Remember to set size_hint=None,None for the widgets added to the layout. i.e. the label widgets.