make a kivy app with a gridlayout of 2*3 size and have square buttons inside. The shape…
- User
make a kivy app with a gridlayout of 2*3 size and have square buttons inside. The shape of buttons are square always and dynamically resized when the window resized
- Assistant
Here is a sample code for a Kivy app with a GridLayout of size 2*3 containing square buttons that dynamically resize when the window is resized:
from kivy.app import App from kivy.uix.gridlayout import GridLayout from kivy.uix.button import Button class MyGrid(GridLayout): def __init__(self, **kwargs): super(MyGrid, self).__init__(**kwargs) self.cols = 3 self.rows = 2 for i in range(6): button = Button(text=str(i), size_hint=(None, None)) button.bind(size=button.setter('text_size')) self.add_widget(button) class MyApp(App): def build(self): return MyGrid() if __name__ == '__main__': MyApp().run()In this code, we create a custom GridLayout class
MyGridwith 2 columns and 3 rows. We then iterate over 6 times to create square buttons with a numeric label from 0 to 5. We set thesize_hintproperty of the buttons to(None, None)so that the buttons don't automatically resize according to the window size. Instead, we bind the button's size to itstext_sizeso that the button size always fits the text it contains.