""" GridLayout为网格布局为了部件为多行距阵 """ from kivy.uix.gridlayout import GridLayout from kivy.app import App from kivy.uix.button import Button from kivy.graphics import Color, Rectangle class GridLayoutWidget(GridLayout): def __init__(self, **kwargs): super().__init__(**kwargs) # 设置背景色 with self.canvas: Color(1, 1, 1, 1) self.rect = Rectangle(pos=self.pos,size=self.size) self.bind(pos=self.update_rect, size=self.update_rect) # 指定列数行为rows self.cols= 10 # 添加按扭 for i in range(50): btn = Button(text="bt"+str(i)) self.add_widget(btn) def update_rect(self, *args): self.rect.pos = self.pos self.rect.size = self.size class GridLayoutApp(App): def build(self): return GridLayoutWidget() if __name__ == '__main__': GridLayoutApp().run()