Python实现
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class MyGridLayout(GridLayout):
def __init__(self, **kwargs):
super(MyGridLayout, self).__init__(**kwargs)
self.cols = 1 # 设定1列
# 添加按钮并指定位置、尺寸和边距
self.add_widget(Button(text='1', pos=(100, 100), size=(50, 50), padding=(10, 10)))
self.add_widget(Button(text='2', pos=(150, 150), size=(50, 50), padding=(10, 10)))
self.add_widget(Button(text='3', pos=(200, 200), size=(50, 50), padding=(10, 10)))
class MyApp(App):
def build(self):
return MyGridLayout()
if __name__ == '__main__':
MyApp().run()
KV实现
from kivy.app import App
from kivy.lang import Builder
Builder.load_string('''
<MyGridLayout>:
cols: 1 # 设定1列
Button:
text: '1'
pos: 100, 100
size: 50, 50
padding: 10, 10
Button:
text: '2'
pos: 150, 150
size: 50, 50
padding: 10, 10
Button:
text: '3'
pos: 200, 200
size: 50, 50
padding: 10, 10
''')
class MyGridLayout(GridLayout):
pass
class MyApp(App):
def build(self):
return MyGridLayout()
if __name__ == '__main__':
MyApp().run()